MilesNepo
July 7th, 2001, 06:31 AM
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".
d.paulson
July 7th, 2001, 07:27 AM
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
Cimperiali
July 9th, 2001, 02:52 AM
'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: KPDTeam@Allapi.net
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.
MilesNepo
July 15th, 2001, 10:44 AM
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.