Before the occurrence of the registry in 32 bit Windows (Windows 95), if you wanted to save any form of application settings, such as the initial window size, you used INI files. Most applications store this information in the Windows registry now, however, you may find it easier to use INI files for now. One advantage of using INI files, is you can easily edit the files manually and also, any settings can easily be removed. INI files use the following structure:
[SectionName]
KeyName1 = KeyValue
KeyName2 = KeyValue
For an example of an INI files, look at the Win.ini file stored in your windows directory. The following code shows how to read and write to an INI file. Create a new project and go Projects | Add Module to add a new module.
Then add the following code.
'// VB Web Code Example
'// www.vbweb.co.uk
' DLL declarations
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
'// Functions
Function GetFromINI(sSection As String, sKey As String, sDefault As String,
sIniFile As String)
Dim sBuffer As String, lRet As Long
' Fill String with 255 spaces
sBuffer = String$(255, 0)
' Call DLL
lRet = GetPrivateProfileString(sSection, sKey, "",
sBuffer, Len(sBuffer), sIniFile)
If lRet = 0 Then
' DLL failed, save default
If sDefault <> ""
Then AddToINI sSection, sKey, sDefault, sIniFile
GetFromINI = sDefault
Else
' DLL successful
' return string
GetFromINI = Left(sBuffer, InStr(sBuffer,
Chr(0)) - 1)
End If
End Function
'// Returns True if successful. If section does not
'// exist it creates it.
Function AddToINI(sSection As String, sKey As String, sValue As String, sIniFile
As String) As Boolean
Dim lRet As Long
' Call DLL
lRet = WritePrivateProfileString(sSection, sKey, sValue,
sIniFile)
AddToINI = (lRet)
End Function
You can then use this code to write and read from the ini files.
Comments