Finally, ten years later, I've made available what I consider to be a fairly
useful little drop-in INI file manipulation class. Originally, this kpINI sample
was offered in 16-bit form for VB3 on CompuServe's old MSBASIC forum. And there
it was one of the most-downloaded samples available. But I never really offered
anything comparable for Win32, and I can't totally defend that. I guess I was
just never happy myself with having adequately abstracted this most simple of
all configuration options. Well, I think this one is now pretty nice.
What I'm offering is a VB5/6/A class module that you can drop right into any
project, and begin using immediately. You instantiate it, tell it the
name/location of the INI file you want to manipulate, and begin reading and/or
writing. For discovery purposes, the class can iterate through entire files and
within each section therein, raising events for every element it encounters. You
can query specific entries in specific sections, specifying default values as
you wish. Values are returned as String by default, but methods are offered to
easily reinterpret them as numeric or boolean instead.
nOpt = ini.ToLong(ini.EntryRead("Options", "0", "General"))
The class caches the current file and section. This means that if you
read/write a number of entries from a single section in succession, you need
only specify the section on the first call. Entire sections can be read into a
Variant array, even. I often use this to get a list of all the entries in the
section, then process each one in turn.
Dim i As Long
Dim arr As Variant
Dim Dest As String
' Look at all the files we need to copy.
arr = ini.SectionRead(False, "Files")
If IsArray(arr) = False Then Exit Function
' Loop through each entry in this section
For i = LBound(arr) To UBound(arr)
Dest = ini.EntryRead(arr(i))
' Code to this copy file...
Next i
INI file comments are denoted by semi-colons, and automatically stripped from
each entry if desired. It would be fairly trivial to add another method to the
class which would accept alternate comment characters, if for some reason the
semi-colon wasn't appropriate.