-
Werken met een inifile
Posted on October 31st, 2008 1 commentIn het ‘oude’ visual basic werkte ik regelmatig met een inifile waarin veel settings stonden.
Omdat ik af en toe wel bezoek over de vloer krijg met als zoektermen visual basic6 of bestanden + visual basic geef ik hier graag de werkwijze weer om gegevens van en naar een inifile te schrijven.Declaratie van functies:
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
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
Gegevens uit een inifile halen:
Public Function sGetINI(sINIfile As String, sSection As String, sKey _
As String, sDefault As String) As StringDim sTemp As String * 256
Dim nLength As IntegersTemp = Space$(256)
nLength = GetPrivateProfileString(sSection, sKey, sDefault, sTemp, _
255, sINIfile)
sGetINI = Left$(sTemp, nLength)End Function
Gegevens naar een inifile schrijven
Public Sub writeINI(sINIfile As String, sSection As String, sKey _
As String, sValue As String)Dim n As Integer
Dim sTemp As StringsTemp = sValue
'Replace any CR/LF characters with spaces
For n = 1 To Len(sValue)
If Mid$(sValue, n, 1) = vbCr Or Mid$(sValue, n, 1) = vbLf _
Then Mid$(sValue, n) = " "
Next nn = WritePrivateProfileString(sSection, sKey, sTemp, sINIfile)
End Sub



Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.