A Class for Easy INI File Handling

WEBINAR:On-Demand

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 <> "" .... )

Delete Functions for INI Class

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