Discussion:
Reading INI files?
(too old to reply)
Cecelia
2004-12-30 14:33:09 UTC
Permalink
Hello all:
I 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.

Thanks in advance.
Mike D Sutton
2004-12-30 14:42:51 UTC
Permalink
Post by Cecelia
I 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.
Have a look at this old post on the subject:
http://groups.google.co.uk/groups?selm=OcAJycxQEHA.132%40TK2MSFTNGP09.phx.gbl
The GetINISectionNames() and GetININames() functions are what you're after here.
There's also this more recent post which demonstrates how to read and write INI files:
http://groups.google.co.uk/groups?selm=Ove0b1Q6EHA.3124%40TK2MSFTNGP11.phx.gbl
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: ***@mvps.org
WWW: Http://EDais.mvps.org/
Veign
2004-12-30 14:43:40 UTC
Permalink
INI file wrapper module
http://www.veign.com/vrc_codeview.asp?type=app&id=64
--
Chris Hanscom - Microsoft MVP (VB)
Veign's Resource Center
http://www.veign.com/vrc_main.asp
--
Post by Cecelia
I 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.
Thanks in advance.
Cecelia
2004-12-30 15:37:04 UTC
Permalink
Thanks to all of you. I think that's what I'm looking for!
Post by Veign
INI file wrapper module
http://www.veign.com/vrc_codeview.asp?type=app&id=64
--
Chris Hanscom - Microsoft MVP (VB)
Veign's Resource Center
http://www.veign.com/vrc_main.asp
--
Post by Cecelia
I 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
Post by Cecelia
data only. If there are "x" amount of sections in a file, how can I just
get
Post by Cecelia
the all the section names for example. Any code examples would be very
helpful as I'm not an expert programmer.
Thanks in advance.
Bob Butler
2004-12-30 14:55:57 UTC
Permalink
Post by Cecelia
I 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..."
Loading...