Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1449

[VB6] INI file class (unicode aware)

$
0
0
'Mainly intended for caching data beetween read-write operations
'Supports UTF-16 LE ini-files format
'Provides wide range of methods
'Doesn't support reading / saving commentary in ini file

Based on Scripting.Dictionary, see also:
#Const UseHashtable
#Const UseStringBuilder

Examples of using:
Code:


Option Explicit

Private Sub Form_Load()
    Dim Item

    'init
    Dim cIni As clsIniFile
    Set cIni = New clsIniFile

    'open ini file
    cIni.InitFile App.Path & "\some.ini", 1200 '1200 - UTF16-LE or 1251 (ANSI)

    'set case insensitive mode
    cIni.CompareMethod = vbTextCompare

    'write (or overwrite):
    '[Section1]
    'Param1=Data1
    'Param2=Data2
    cIni.WriteParam "Section1", "Param1", "Data1"
    cIni.WriteParam "Section1", "Param2", "Data2"

    'create empty section
    cIni.CreateSection "Section Empty1"
    cIni.CreateSection "Section Empty2"

    Debug.Print "Param1 = " & cIni.ReadParam("Section1", "Param1")
    Debug.Print "Number of parameters in Section1: " & cIni.CountParams("Section1")
    Debug.Print "Total sections: " & cIni.CountSections

    'does data 'Data1' exist in 'Section1' ?
    Debug.Print "Data2 exists? " & cIni.ExistData("Section1", "Data2")
    Debug.Print "param2 exists? " & cIni.ExistParam("Section1", "param2")

    Debug.Print "Currently loaded filename is: " & cIni.FileName

    'search for parameter name, which holds a 'Data2' in 'Section1'
    Debug.Print "Param name of 'Data2' is: " & cIni.GetParamNameByData("Section1", "Data2")

    'enum parameters' names
    For Each Item In cIni.GetParamNames("Section1")
        Debug.Print Item
    Next
    'enum data in section
    For Each Item In cIni.GetParamValues("Section1")
        Debug.Print Item
    Next
    'enum sections' names
    For Each Item In cIni.GetSections
        Debug.Print Item
    Next

    'to remove a parameter
    If cIni.RemoveParam("Section1", "Param2") Then Debug.Print "Param2 is removed successfully!"

    'to remove section
    If cIni.RemoveSection("Section Empty2") Then Debug.Print "'Section Empty2' is removed successfully!"

    'to remove all sections (erase file)
    'cIni.RemoveSectionsAll

    'populate physical file (all cached data will by written to the disk)
    cIni.Flush

    'when you finished work with the class
    Set cIni = Nothing

    Unload Me
End Sub


Result:
Quote:

Param1 = Data1
Number of parameters in Section1: 2
Total sections: 3
Data2 exists? True
param2 exists? True
Currently loaded filename is: H:\_AVZ\Íàøè ðàçðàáîòêè\_Dragokas\clsIniFile\some.ini
Param name of 'Data2' is: Param2
Param1
Param2
Data1
Data2
Section1
Section Empty1
Section Empty2
Param2 is removed successfully!
'Section Empty2' is removed successfully!
Attached Files

Viewing all articles
Browse latest Browse all 1449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>