clsINI.cls

 VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsINI"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Const sEmpty = ""

Public INI_FileName As String
Public Enum EDataType
    dtString
    dtBoolean
    dtNumeric
End Enum

Public Function GetString(SectionHeader As String, VariableName As String, Optional Default As String = "", Optional DataType As EDataType = dtString) As String
'get a value from the INI file
Dim FSO As New Scripting.FileSystemObject, INIFile As Scripting.TextStream
Dim A$
Dim ret As String
    Set FSO = New Scripting.FileSystemObject
   'Get requested information, trimming the returned
   If Not FSO.FileExists(INI_FileName) Then A$ = Default: GoTo NoFile
   Set INIFile = FSO.OpenTextFile(INI_FileName, ForReading)
   ret = String(255, vbNullChar)
   Call GetSectional(SectionHeader, VariableName, Default, ret, INIFile)
   A$ = ret

   Select Case DataType
   Case dtString
      'do nothing as this is default
   Case dtBoolean
      'verify value contains true or false, default is true unless otherwise specified
      Dim B$
      B$ = UCase(A$)
      If Default = sEmpty Then Default = "True"
      If B$ <> "TRUE" And B$ <> "FALSE" Then A$ = Default
   Case dtNumeric
      'verify the value is a number
      If Val(A$) = 0 Then A$ = Default
   End Select
NoFile:
   'return the value
   GetString = A$
Hell:
End Function

Public Function SaveString(SectionHeader As String, VariableName As String, Value As String) As Integer
Dim FSO As New Scripting.FileSystemObject, INIFile As Scripting.TextStream
Dim bFound As Boolean, tmp As String, tmp1 As String, tmp2 As String, iPos As Long
    Set FSO = New Scripting.FileSystemObject
    If Not FSO.FileExists(INI_FileName) Then GoTo NoFile
    Set INIFile = FSO.OpenTextFile(INI_FileName, ForReading)
    tmp = INIFile.ReadAll
    If InStr(tmp, "[" & SectionHeader & "]") <> 0 Then
        tmp1 = Mid(tmp, 1, InStr(tmp, "[" & SectionHeader & "]") + Len("[" & SectionHeader & "]") + 1)
        tmp2 = Mid(tmp, InStr(tmp, "[" & SectionHeader & "]") + Len("[" & SectionHeader & "]") + 1)
        INIFile.Close
        Set INIFile = FSO.OpenTextFile(INI_FileName, ForWriting)
        If InStr(tmp2, VariableName) <> 0 Then
            iPos = InStr(tmp2, VariableName)
            iPos = InStr(iPos, tmp2, vbCrLf)
            tmp2 = Mid(tmp2, iPos)
        End If
        INIFile.Write tmp1
        INIFile.Write VariableName & "=" & Value & vbCrLf
        INIFile.Write tmp2
    Else
        INIFile.Close
NoFile:
        Set INIFile = FSO.OpenTextFile(INI_FileName, ForAppending, True)
        INIFile.Write "[" & SectionHeader & "]" & vbCrLf
        INIFile.Write VariableName & "=" & Value & vbCrLf
    End If
    INIFile.Close
End Function

Private Function GetSectional(Section As String, Variable As String, Default As String, ret As String, ByRef INIFile As Scripting.TextStream) As Long
Dim iStart As Long, iEnd As Long, tmp As String
    tmp = INIFile.ReadAll
    iStart = InStr(tmp, "[" & Section & "]") + Len("[" & Section & "]")
    iStart = InStr(iStart, tmp, Variable & "=") + Len(Variable + "=")
    iEnd = InStr(iStart, tmp, vbCrLf)
    tmp = Mid(tmp, iStart, iEnd - iStart)
    If tmp = "" Then tmp = Default
    ret = tmp
End Function

Project Homepage: