Microsoft & .NETVisual BasicSimple INI File Class

Simple INI File Class

INI Files now seem to have been completely removed
by the registry, but sometimes they are a much better option. This can be becuase of
safety for the registry, and allowing the user to more easily view and change settings.
This simple wrapper class wraps up a few simple INI file functions into a reusable, drop
in class for instant INI file use. It supports the following methods/properties:

  • CreateINI – Creates a new INI file
  • GetFile – Returns the value specified in the arguements passed
  • INIFile – Gets/Sets the current INI file path
  • MakePath – A private function used to create the ini file
  • WriteFile – Writes a value to the INI file

You will probably want to extend its functionality by adding more INI file functions.
Anyway, the class is based around two Win API declares: WritePrivateProfileString and
GetPrivateProfileString. Both require a bit of extra work to make them effective, but
overall they are quite simple to use. A good alternative to the Windows Registry.

To get the class working just use:

Private m_cIni As CIniFile
Private Sub Form_Load()
  Set m_cIni = New CIniFile
End Sub

Add a new class module (CIniFIle) and copy the following
code into it:

Option Explicit

'// Private member that holds a reference to
'// the path of our ini file
Private strInI As String

'// Win API Declares
Private Declare Function WritePrivateProfileString _
  Lib "kernel32" Alias "WritePrivateProfileStringA" _
 (ByVal lpApplicationName As String, _
  ByVal lpKeyName As Any, _
  ByVal lpString As Any, _
  ByVal lpFileName As String) As Long

Private Declare Function GetPrivateProfileString _
  Lib "kernel32" Alias "GetPrivateProfileStringA" _
 (ByVal lpApplicationName As String, _
  ByVal lpKeyName As Any, _
  ByVal lpDefault As String, _
  ByVal lpReturnedString As String, _
  ByVal nSize As Long, _
  ByVal lpFileName As String) As Long

Private Function MakePath(ByVal strDrv As String, _
  ByVal strDir As String) As String
 
 '// Makes an INI file: Guarantees a sub dir
 Do While Right$(strDrv, 1) = ""
   strDrv = Left$(strDrv, Len(strDrv) - 1)
 Loop

 Do While Left$(strDir, 1) = ""
   strDir = Mid$(strDir, 2)
 Loop

 '// Return the path
 MakePath = strDrv & "" & strDir
End Function

Public Sub CreateIni(strDrv As String, strDir As String)


 '// Make a new ini file
 strInI = MakePath(strDrv, strDir)
End Sub

Public Sub WriteFile(strSection As String, _
 strKey As String, _
 strValue As String)
 
 '// Write to strINI
 WritePrivateProfileString strSection, _
   strKey, strValue, strInI
End Sub

Public Function GetFile(strSection As String, _
  strKey As String) As String

  Dim strTmp As String
  Dim lngRet As String

  strTmp = String$(100, 0)
  lngRet = GetPrivateProfileString(strSection, _
    strKey, "", strTmp, _
    Len(strTmp), strInI)
    
  GetFile = strTmp
 
End Function

Public Property Let INIFile(ByVal New_IniPath As String)

 '// Sets the new ini path
 strInI = New_IniPath
 
End Property

Public Property Get INIFile() As String

 '// Returns the current ini path
 INIFile = strInI
 
End Property
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.

Latest Posts

Related Stories