The new vbAccelerator Site - more VB and .NET Code and Controls
Source Code
3 Code Libraries Source Code &nbsp


 NOTE: this code has been superceded by the version at the new site.



&nbsp

An Easy INI File Class

VB INI File Class Demo

Download the cIniFile Class (3kb)

Download the cIniFile Demo Application (27kb)

The cIniFile class is an easy, self-contained way to get complete access to INI files. Although use of INI files is no longer recommended under Windows (you should use the registry instead - see my Registry class for an easy migration from this method of using Ini files to using the registry) they are ultra simple to use. They also make it simpler to ship your application.

The class allows you to create, enumerate and delete sections and keys within an INI file, without restriction. Enumerating keys and sections in an INI file is quite a handy technique, as it means you can create INI files with arbitrary sections and keys and interrogate them easily. See the cIniFile.cls code to see how you do it - its just a simple (but little known) mod to the VB API declaration for GetPrivateProfileString (thanks to Jonathan Hopkins for passing this information on).

Here is a brief summary of typical uses of the class:

To get Value from the Ini File

&nbsp &nbsp With m_cIni
&nbsp &nbsp &nbsp &nbsp .Path = App.Path & "\TEST.INI"
&nbsp &nbsp &nbsp &nbsp .Section = "Options"
&nbsp &nbsp &nbsp &nbsp .Key = "SavePath"
&nbsp &nbsp &nbsp &nbsp .Default = App.Path
&nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp sSavePath = .Value
&nbsp &nbsp &nbsp &nbsp If Not (.Success) Then
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp MsgBox "Failed to get value.", vbInformation
&nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp End If

To Write a Value to the Ini file

&nbsp &nbsp With m_cIni
&nbsp &nbsp &nbsp &nbsp .Path = App.Path & "\TEST.INI"
&nbsp &nbsp &nbsp &nbsp .Section = "Options"
&nbsp &nbsp &nbsp &nbsp .Key = "SavePath"
&nbsp &nbsp &nbsp &nbsp .Default = App.Path
&nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp .Value = sSavePath
&nbsp &nbsp &nbsp &nbsp If Not (.Success) Then
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp MsgBox "Failed to save value.", vbInformation
&nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp End If

To Get All the Sections within an Ini File

Dim sSections() As String
Dim iSectionCount As Long

&nbsp &nbsp With m_cIni
&nbsp &nbsp &nbsp &nbsp .EnumerateAllSections sSections(), iSectionCount
&nbsp &nbsp &nbsp &nbsp For iSection = 1 To iSectionCount
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp lstIni.AddItem "[" & sSections(iSection) & "]"
&nbsp &nbsp &nbsp &nbsp Next iSection
&nbsp &nbsp End With

To Get All the Keys within a section of an Ini File

Dim sKeys() As String
Dim iKeycount As Long

&nbsp &nbsp .Section = "Options"
&nbsp &nbsp .EnumerateCurrentSection sKeys(), iKeycount
&nbsp &nbsp For iKey = 1 To iKeycount
&nbsp &nbsp &nbsp &nbsp .Key = sKeys(iKey)
&nbsp &nbsp &nbsp &nbsp Debug.Print .Key & "=" & .Value
&nbsp &nbsp Next iKey

To Delete a Key from an INI file

&nbsp &nbsp With m_cIni
&nbsp &nbsp &nbsp &nbsp .Path = App.Path & "\TEST.INI"
&nbsp &nbsp &nbsp &nbsp .Section = "Options"
&nbsp &nbsp &nbsp &nbsp .Key = "SavePath"
&nbsp &nbsp &nbsp &nbsp .DeleteKey
&nbsp &nbsp &nbsp &nbsp If Not (.Success) Then
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp MsgBox "Delete Key Failed.", vbInformation
&nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp End With

To Delete a Section from an INI file

&nbsp &nbsp With m_cIni
&nbsp &nbsp &nbsp &nbsp .Path = App.Path & "\TEST.INI"
&nbsp &nbsp &nbsp &nbsp .Section = "Options"
&nbsp &nbsp &nbsp &nbsp .DeleteSection
&nbsp &nbsp &nbsp &nbsp If Not (.Success) Then
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp MsgBox "Delete Section Failed.", vbInformation
&nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp End With



Back to top

Back to Source Code Overview

&nbsp
 

About  Contribute  Send Feedback  Privacy

Copyright © 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 15 June 1998