A Class for Easy INI File Handling
CDS_CIni: an easy wrapper for ini file handling
It has 2 properties:
FileName : the ini filename. Without this, nothing will work
LastErrorDescription : the description of the last error if some error has occured
And it has the following methods:
ReadValue : reads the specified value from the inifile. As input it wants the Section name, Key name, and a string which will contain the Value on return.
It returns true if success, false if fails (get the LastErrorDescription for more info.....)
ReadSection : reads specifed section at once, input is section name, an 2 arrays, 1 to retreive the key names, the other to retreive the values.
It returns true if success, false if fails (get LastErrorDescription for more info.....)
NOTE that both arrays must not be fixed dimension, else the redim will fail!!!
WriteValue : writes the specified value to the ini file pointed to in the Filename property
Input is the Section name, Key name, and the Value.
It returns true if success, false if fails (get LastErrorDescription for more info.....)
WriteSection : Write a section at once. Input are the Section name, an array of keys, and an array of values. Both array must be of the same size, else nothing will be written.
It returns true if success, false if fails (get LastErrorDescription for more info.....
Enum_SectionNames : get all section names in the ini file. Input is an array which will contain the section names on return.
It returns true if success, false if fails (get LastErrorDescription for more info.....
Oh why I made the return values boolean? Since I love function of which you easily can check if they were succesful in an if statement (I thought about returning the string but then you have to check if the string <> "" .... )
Sample usuage:
Dim cIni as new CDS_CIni
cIni.FileName = "C:\Dummy.ini"
cIni.WriteValue "Section 1", "MyKey 1", "MyValue 1"
cIni.WriteValue "Section 1", "MyKey 2", "MyValue 2"
The ini file looks now like:
[Section 1]
MyKey 1=MyValue 1
MyKey 2=MyValue 2
Dim sValue as string
cIni.ReadValue "Section 1", "MyKey 1", sValue
Debug.print "Value of MyKey 1 = " & sValue
cIni.ReadValue "Section 1", "MyKey 2", sValue
Debug.print "Value of MyKey 2 = " & sValue
The debug window now shows:
Value of MyKey 1 = MyValue 1
Value of MyKey 2 = MyValue 2
Cool, since that were the values we just wrote in the ini file :-)
Dim arrKeys() as string, arrValues() as string
ReDim arrKeys(2)
ReDim arrValues(2)
arrKeys(0) = "Key 1"
arrKeys(1) = "Key 2"
arrKeys(2) = "Key 3"
arrValues(0) = "Value 1"
arrValues(0) = "Value 2"
arrValues(2) = "Value 3"
cIni.WriteSection "Section 2", arrKeys(), arrValues()
We now just wrote a whole section at once.... the ini file looks now like:
[Section 1]
MyKey 1=MyValue 1
MyKey 2=MyValue 2
[Section 2]
Key 1=Value 1
Key 2=Value 2
Key 3=Value 3
Now read a whole section at once:
cIni.ReadSection "Section 2", arrKeys(), arrValues()
Dim i as Integer
Debug.print "Keys and values of Section 2:"
for i = LBound(arrKeys) to UBound(arrKeys)
Debug.print arrKeys(i) & "=" & arrValues(i)
next
The debug window shows this so it works :-)
Keys and values of Section 2:
Key 1=Value 1
Key 2=Value 2
Key 3=Value 3
And in case you don't know which sections there are in an ini file:
Dim sSections() as string
cIni.Enum_SectionNames sSections()
for i = LBound(sSections) to UBound(sSections)
Debug.print sSections(i)
next i
Section 1
Section 2
And these are indeed the 2 sections we have :-)
Well this is it... easy huh? :-)
Have fun :-)
Crazy D :-)
Download CDI_INI Class Module (11k)

Comments
Please help me out downloading this "CDS_CIni " article
Posted by Pigel on 08/23/2004 04:02amHi again, please help me downloading this article 'A Class for Easy INI File Handling' or send me the file "1772_CDS_CIni.cls" Many, many thanks !!!
Replyhelp me to download!
Posted by vuquan on 05/10/2004 08:33amhttp://www.codeguru.com/vb/controls/vb_activex/article.php/c3485/1772_CDS_CIni.cls is a null text file! Pls, help me to get it! Thanks a lot
ReplyA Class for Easy INI File handling
Posted by Legacy on 05/15/2003 12:00amOriginally posted by: Daniel Bruce
Many thanks, you have saved me hours of work.
ReplyComment
Posted by Legacy on 03/27/2003 12:00amOriginally posted by: Elangovan.D
ReplyThanks!!!
Posted by Legacy on 01/07/2003 12:00amOriginally posted by: Ramin
It was really helpfull. I had my own class but yours is shorter and abvious and also works perfect!!! Thank you.
ReplyDelete Key and Delete Section from INI Files
Posted by Legacy on 01/03/2003 12:00amOriginally posted by: ARS
ReplyHow Do I Modify?
Posted by Legacy on 10/17/2002 12:00amOriginally posted by: Mauricio
I need to know pls how can I modify a name, key or value
ReplyPls , i�m new on this
THNX
A Class for Easy INI File handling
Posted by Legacy on 09/14/2002 12:00amOriginally posted by: uday j yadurkar
ReplyDelete Functions for INI Class
Posted by Legacy on 04/15/2002 12:00amOriginally posted by: Paul Adams
Here is some code that some might find useful. Add it to the INI Class module.
Public Function DeleteKey(psSection As String, psKey As String) As Boolean
' get out if we don't have a filename ....
If Not HaveFileName Then DeleteKey = False: Exit Function
' Return value
Dim lRet As Long
' Try to write the value
lRet = WritePrivateProfileString(psSection, psKey, 0&, m_FileName)
' if lRet = 0 an error has occured
If lRet = 0 Then GetTheLastError
DeleteKey = CBool(lRet)
End Function
Public Function DeleteSection(psSection As String) As Boolean
' get out if we don't have a filename ....
If Not HaveFileName Then DeleteSection = False: Exit Function
' Return value
Dim lRet As Long
' Try to write the value
lRet = WritePrivateProfileString(psSection, 0&, 0&, m_FileName)
' if lRet = 0 an error has occured
If lRet = 0 Then GetTheLastError
DeleteSection = CBool(lRet)
End Function
Paul
ReplyAsk help!I really need the code, but download doesn't work well
Posted by Legacy on 02/01/2002 12:00amOriginally posted by: XW
I really need the code, but download doesn't work well.
ReplyLoading, Please Wait ...