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)