PDA

Click to See Complete Forum and Search --> : reading all headers from ini files


mgoarrow
Jul 31st, 2001, 03:57 PM
I'm familiar and have used the GetProfileString and WriteProfileString API calls to read and write from .ini files. Is there a way to retrieve the names of all headers in an .ini file and the name and values of the existing keys under each header?

Thanks for your help!

TheBao
Jul 31st, 2001, 05:52 PM
Ini file is just a text file. You know all the header are inside []. Just read the file as you normally read the text file and search for all [].

Regards.

mgoarrow
Aug 1st, 2001, 12:28 PM
True, but I'm looking to use the API to gather the header information - not the standard Open File for input stuff.....My API guide didn't have the API call I was looking for but I've since found it - it's the GetPrivateProfileSectionNames function. The API is much faster than the open file routine - thanks for your help!

dubae524
Aug 5th, 2001, 10:19 AM
Yes. But which header information. The headers themselves? The keys, or the values? I have written a lot of non-API code on INI files. I'm right now writing instructions for using the modules, but I can post different functions if you need any help. :)

mgoarrow
Aug 6th, 2001, 04:17 AM
Here' s what I ended up doing (I used a TreeView control to display the info):


Option Explicit

Private Declare Function GetPrivateProfileSectionNames Lib "kernel32" _
Alias "GetPrivateProfileSectionNamesA" (ByVal lpReturnBuffer As String, _
ByVal nSize As Long, ByVal lpName As String) As Long
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias _
"GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString _
As String, ByVal nSize As Long, ByVal lpFileName As String) As Long


Public Sub FillTree(strPath As String, objForm As Object)

Dim strBuffer As String
Dim lngRet As Long
Dim arrHeaders() As String
Dim arrValues() As String
Dim I, J As Integer
Dim nodParent As Node

strBuffer = Space(1000)

lngRet = GetPrivateProfileSectionNames(strBuffer, Len(strBuffer), strPath)
If lngRet Then
arrHeaders = Split(strBuffer, vbNullChar)
For I = 0 To UBound(arrHeaders)
Set nodParent = objForm.TreeView1.Nodes.Add(, , arrHeaders(I), arrHeaders(I))
nodParent.Expanded = True

strBuffer = Space(1000)
lngRet = GetPrivateProfileSection(arrHeaders(I), strBuffer, Len(strBuffer), _
strPath)

If lngRet Then
strBuffer = Replace(strBuffer, vbNullChar & vbNullChar, vbNullChar)
arrValues = Split(strBuffer, vbNullChar)

For J = 0 To UBound(arrValues)
If Len(Trim(arrValues(J))) Then
objForm.TreeView1.Nodes.Add nodParent, tvwChild, , arrValues(J)
End If
Next
End If
Next
End If

End Sub