Post by CeceliaI am using VB6.0 and am trying to understand a practical way to
"read" all the data in an ini file. So far I understand there are
sections, keywords, and values in an ini file. I have found examples
on google searches for adding and deleting items, but I want to read
and work with the data only. If there are "x" amount of sections in
a file, how can I just get the all the section names for example.
Any code examples would be very helpful as I'm not an expert
programmer.
This should get you started
Private Declare Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringA" _
(ByVal grpnm As String, ByVal parnm As String, _
ByVal deflt As String, ByVal parvl As String, _
ByVal parlen As Long, ByVal INIPath As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringA" _
(ByVal grpnm As String, ByVal parnm As String, _
ByVal parvl As String, ByVal INIPath As String) As Long
Public Function INIGetValue(ByVal INIPath As String, _
ByVal SectionName As String, ByVal KeyName As String, _
ByVal DefaultValue As String) As String
Dim sBuff As String
Dim x As Long
sBuff = Space$(1024)
x = GetPrivateProfileString(SectionName, KeyName, DefaultValue, _
sBuff, Len(sBuff), INIPath)
INIGetValue = Left$(sBuff, x)
End Function
Public Sub INISetValue(ByVal INIPath As String, _
ByVal SectionName As String, ByVal KeyName As String, _
ByVal KeyValue As String)
Dim x As Long
x = WritePrivateProfileString(SectionName, KeyName, KeyValue, INIPath)
End Sub
Public Function INIGetSections(ByVal INIPath As String) As String()
Dim sBuff As String
Dim x As Long
sBuff = Space$(8192)
x = GetPrivateProfileString(vbNullString, vbNullString, vbNullString, _
sBuff, Len(sBuff), INIPath)
sBuff = Left$(sBuff, x)
If Right$(sBuff, 1) = vbNullChar Then sBuff = Left$(sBuff, Len(sBuff) - 1)
INIGetSections = Split(sBuff, vbNullChar)
End Function
Public Function INIGetKeys(ByVal INIPath As String, _
ByVal SectionName As String) As String()
Dim sBuff As String
Dim x As Long
sBuff = Space$(8192)
x = GetPrivateProfileString(SectionName, vbNullString, vbNullString, _
sBuff, Len(sBuff), INIPath)
sBuff = Left$(sBuff, x)
If Right$(sBuff, 1) = vbNullChar Then sBuff = Left$(sBuff, Len(sBuff) - 1)
INIGetKeys = Split(sBuff, vbNullChar)
End Function
Sub main()
DumpINI "win.ini"
End Sub
Private Sub DumpINI(ByVal INIPath As String)
Dim sSec() As String
Dim sKey() As String
Dim s As Long
Dim k As Long
sSec = INIGetSections(INIPath)
For s = LBound(sSec) To UBound(sSec)
Debug.Print "Section: " & sSec(s)
sKey = INIGetKeys(INIPath, sSec(s))
For k = LBound(sKey) To UBound(sKey)
Debug.Print " Key=" & sKey(k);
Debug.Print " Value=" & INIGetValue(INIPath, sSec(s), sKey(k), "")
Next
Next
End Sub
--
Reply to the group so all can participate
VB.Net: "Fool me once..."