View back issues of Code of the Week online at http://www.codeoftheweek.com/issues
See our web site http://www.codeoftheweek.com/links/specialoffer.html for an exciting subscription offer. You do not want to miss this one.
This issue was designed for VB 4.0 and up. It is compatible with 16 bit and 32 bit versions of Visual Basic. Questions? Email us at firstname.lastname@example.org.
The complete source code appears near the end of this issue. Be sure not to miss it!
The class in this issue provides a very easy to use interface to the PrivateProfile API calls (the calls which allow you to read and write INI files). We know that many applications write information to the registry instead of INI files nowadays, but we feel INI files are still much easier to manage and can be modifed much easier with a text editor (if necessary).
There are several functions and properties in this class. There are three groups are properties to define what INI file and section to use, functions to read INI files and functions to write INI files.
Property Filename - Read/Write - Specifies the INI filename to use, for example: Ini.Filename = App.Path & "\TEST.INI"
Property Section - Read/Write - Specifies the section within a filename to use, for example: Ini.Section = "Paths"
There are 16-bit versions and 32-bit versions of the following functions. The only difference is that the 32-bit versions use Long values instead of Integers. We will show the 32-bit versions here. The full source code contains both versions.
Public Function ReadInt(sKey As String, iValue As Long, iDefault As Long) As Long Public Function ReadString(sKey As String, sValue As String, sDefault As String) As Long Public Function WriteInt(sKey As String, iValue As Long) As Long Public Function WriteString(sKey As String, sValue As String) As Long
All functions return True upon success and False upon failure.
Example 1 - How to retrieve a string from the INI file
Dim Ini As New cIniFile Ini.Filename = App.Path & "\MYAPP.INI" ' get database path Ini.Section = "Paths" If Not Ini.ReadString("DatabasePath", gsDBPath, App.Path) Then MsgBox "Can not determine the DatabasePath, can not continue application." Exit Sub End If
Example 2 - How you can use INI files to save and restore the window position. The first routine should appear in the Form_Load event and the second should appear in the Form_Unload event
Private Sub Form_Load() Dim Ini As New cIniFile Dim lValue As Long Ini.Filename = App.Path & "\MYAPP.INI" Ini.Section = "Options" ' restore window position - force to upper left if no position exists Ini.ReadInt "WindowTop", lValue, 0 Me.Top = lValue Ini.ReadInt "WindowLeft", lValue, 0 Me.Left = lValue Set Ini = Nothing End Sub Private Sub Form_Unload(Cancel As Integer) Dim Ini As New cIniFile Ini.Filename = App.Path & "\MYAPP.INI" Ini.Section = "Options" ' save window position Ini.Section = "Options" Ini.WriteInt "WindowTop", Me.Top Ini.WriteInt "WindowLeft", Me.Left Set Ini = Nothing End Sub
To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0063.html