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:02am

    Hi 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 !!!

    Reply
  • help me to download!

    Posted by vuquan on 05/10/2004 08:33am

    http://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

    Reply
  • A Class for Easy INI File handling

    Posted by Legacy on 05/15/2003 12:00am

    Originally posted by: Daniel Bruce

    Many thanks, you have saved me hours of work.

    Reply
  • Comment

    Posted by Legacy on 03/27/2003 12:00am

    Originally posted by: Elangovan.D

    It's really very very Nice.  TanK'S
    

    Reply
  • Thanks!!!

    Posted by Legacy on 01/07/2003 12:00am

    Originally posted by: Ramin

    It was really helpfull. I had my own class but yours is shorter and abvious and also works perfect!!! Thank you.

    Reply
  • Delete Key and Delete Section from INI Files

    Posted by Legacy on 01/03/2003 12:00am

    Originally posted by: ARS

    You can add the following to  the  CDI Class Module to Delete a Key or Delete a Section from the Ini File.
    
    


    Delete a section

    lRet = m_IniFile.WriteValue(LpSectionName, vbNullString, vbNullString)

    Delete a Key

    lRet = m_IniFile.WriteValue(LpSectionName.Text, LpKeyName, vbNullString)

    Reply
  • How Do I Modify?

    Posted by Legacy on 10/17/2002 12:00am

    Originally posted by: Mauricio

    I need to know pls how can I modify a name, key or value
    Pls , i�m new on this
    THNX

    Reply
  • A Class for Easy INI File handling

    Posted by Legacy on 09/14/2002 12:00am

    Originally posted by: uday j yadurkar

    Its very nice code.
    
    this code is very useful to me.

    thanks.
    uday

    Reply
  • Delete Functions for INI Class

    Posted by Legacy on 04/15/2002 12:00am

    Originally 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

    Reply
  • Ask help!I really need the code, but download doesn't work well

    Posted by Legacy on 02/01/2002 12:00am

    Originally posted by: XW

    I really need the code, but download doesn't work well.

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds