CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2001
    Posts
    12

    Can you give me a code on how to use .ini files?

    What are .ini files for?
    A friend of mine told me that you can store the Path of the Executable
    File in .ini files and if this .ini file is opened the path stored in it will
    be used by whoever's calling it.

    Can you give me a code that uses .ini files ?

    I'd like to create a VB .exe file that looks in a .ini file for the Path of
    the File that it has to run. For ex. I need to run "Compact.mdb" and I will place the .mdb in "C:\Access\DB\Compact.mdb".


  2. #2
    Join Date
    Jan 2000
    Location
    Saskatchewan, Canada
    Posts
    595

    Re: Can you give me a code on how to use .ini files?

    To retrieve data

    Dim FF as integer
    Dim strDatabase as string
    FF = freefile
    Open "Yourfile.ini" for input as #FF
    Input #FF,strdatabase
    Close #ff

    To write data

    Dim FF as integer
    Dim strDatabase as string
    strDatabase = "C:\Access\DB\Compact.mdb"
    FF = freefile
    Open "Yourfile.ini" for Output as #FF
    Write #FF,strdatabase
    Close #ff

    But if this is all the information that you are saving, I would suggest using the registry.

    SaveSetting appname, section, key, setting ' to save info

    ex. SaveSetting "YourAppName", "DatabaseNames", "Compacteddata","C:\Access\DB\Compact.mdb"


    GetSetting(appname, section, key[, default]) 'to retrieve info

    ex.
    strDatabase = GetSetting("YourAppName", "DatabaseNames", "Compacteddata")




    David Paulson

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re:Ini files Api


    'Here is a good example from Api Guide:

    private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (byval lpApplicationName as string, byval lpKeyName as Any, byval lpDefault as string, byval lpReturnedString as string, byval nSize as Long, byval lpFileName as string) as Long
    private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (byval lpApplicationName as string, byval lpKeyName as Any, byval lpString as Any, byval lpFileName as string) as Long
    private Sub Form_Load()
    'KPD-Team 1999
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    Dim Ret as string, NC as Long
    'Write the setting to the file (c:\test.ini) under
    ' Project1 -> Keyname
    WritePrivateProfileString App.Title, "KeyName", "This is the value", "c:\test.ini"
    'Create a buffer
    Ret = string(255, 0)
    'Retrieve the string
    NC = GetPrivateProfileString(App.Title, "KeyName", "Default", Ret, 255, "C:\test.ini")
    'NC is the number of characters copied to the buffer
    If NC <> 0 then Ret = Left$(Ret, NC)
    'Show our string
    MsgBox Ret
    'Delete the file
    Kill "c:\test.ini"
    End Sub

    'Getprivate Parameters
    '· lpAppName
    'Points to a null-terminated string that specifies the section containing the key name. If this parameter is null, the GetPrivateProfileString function copies all section names in the file to the supplied buffer.
    '
    '· lpKeyName
    'Pointer to the null-terminated string containing the key name whose associated string is to be retrieved. If this parameter is null, all key names in the section specified by the lpAppName parameter are copied to the buffer specified by the lpReturnedString parameter.
    '
    '· lpDefault
    'Pointer to a null-terminated default string. If the lpKeyName key cannot be found in the initialization file, GetPrivateProfileString copies the default string to the lpReturnedString buffer. This parameter cannot be null.
    'Avoid specifying a default string with trailing blank characters. The function inserts a null character in the lpReturnedString buffer to strip any trailing blanks.
    'Windows 95: Although lpDefault is declared as a constant parameter, Windows 95 strips any trailing blanks by inserting a null character into the lpDefault string before copying it to the lpReturnedString buffer.
    'Windows NT: Windows NT does not modify the lpDefault string. This means that if the default string contains trailing blanks, the lpReturnedString and lpDefault strings will not match when compared using the lstrcmp function.
    '
    '· lpReturnedString
    'Pointer to the buffer that receives the retrieved string.
    '
    '· nSize
    'Specifies the size, in characters, of the buffer pointed to by the lpReturnedString parameter.
    '
    '· lpFileName
    'Pointer to a null-terminated string that names the initialization file. If this parameter does not contain a full path to the file, Windows searches for the file in the Windows directory.
    '
    '-----------------------------------------------------------------------------------------------------------
    'WritePrivate Parameters
    '· lpAppName
    'Points to a null-terminated string containing the name of the section to which the string will be copied. If the section does not exist, it is created. The name of the section is case-independent; the string can be any combination of uppercase and lowercase letters.
    '
    '· lpKeyName
    'Points to the null-terminated string containing the name of the key to be associated with a string. If the key does not exist in the specified section, it is created. If this parameter is null, the entire section, including all entries within the section, is deleted.
    '
    '· lpString
    'Points to a null-terminated string to be written to the file. If this parameter is null, the key pointed to by the lpKeyName parameter is deleted.
    'Windows 95: This platform does not support the use of the TAB (\t) character as part of this parameter.
    '
    '· lpFileName
    'Points to a null-terminated string that names the initialization file.
    '






    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    Jul 2001
    Posts
    12

    Re: Can you give me a code on how to use .ini files?

    No, what I'd like to do is Open an .ini file that contains the Path of where my Inventory.mdb is located.
    Ex. "C:\Databases\Inventory.mdb"

    So what I need now is to create a Visual Basic Form that will open this .ini file and get the Path specified in the .ini file and use it to Compact the Inventory Database. If ever the user would want to change the file to be compacted and which is also located in another directory, the User can do it with changing the VB Form created. Later on, this Visual Basic Form will become an .exe file and if that happens I cannot anymore edit the code, but I can however change the File and Path of whatever I want to Compact.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured