newman     VBWeb     VBSource     Portfolio    

INI.BAS
OVERVIEW
Ini & IniFile properties Value = Ini(Section, Label)
Ini(Section, Label) = Value
Value = IniFile(Section, Label, FileName)
IniFile(Section, Label, FileName) = Value
IsTrue function If IsTrue(Ini(Section, Label)) then ...
IniOnBlankRaiseError variable
IniFileNameDefault variable
IniLabels function Set colLabels = IniFileLabels(Section, FileName)
IniSection function Set colValues = IniSection(Section, FileName)
IniDeleteSection command Call IniDeleteSection(Section$, FileName$)

OVERVIEW

Ini - pronounced innie like a kind of belly button; aka: Configuration Settings File; INI is the file extention of Ini files.

Ini files were used before the Registry came along with 95 & NT. I still prefer to use Ini files since I can just delete them. I wrote this module shortly after VB4 came out. I rarely use any of these procedures I've written here other than the Ini property. And I rarely enter an Ini file name. These procedures pick a nice one for me.

If you want to pick the Ini file name, be sure you understand the difference between the IniFileNameDefault property and the FileName arguments. Basicly, If you fill in a FileName argument, it will not change the value of IniFileNameDefault. The FileName arguments are provided just as a way of temporarilly overriding IniFileNameDefault. See the IniFileNameDefault property documentation for more info.

To use these procedures, be sure to add the INI.BAS file to your project.

Ini & IniFile dynamic properties

Value$ = Ini(Section$, Label$) Ini(Section$, Label$) = Value$ Value$ = IniFile(Section$, Label$, FileName$) IniFile(Section$, Label$, FileName$) = Value$ Returns or sets the value associated with the given label & section. If Ini is used instead of IniFile, then IniFileNameDefault is used as the file name. There is no function to remove labels added beyond setting the label's value to blank.

IsTrue function

If IsTrue(Ini(Section$, Label$)) then ... IsTrue provides an easy way to check for a boolean value. If the value resembles anything in the affirmative (1, -1, T, True, Y, Yes, On), then True is returned. Otherwise, False is returned. False values should look like 0, F, False, N, No, or Off.

IniOnBlankRaiseError static property

This simplefies catching the problem of misspelling label and section names. If IniOnBlankRaiseError is True and any procedure finds only blanks, then an error is raised. IniOnBlankRaiseError is False by default.

Example

Dim Min As Long, Max As Long IniOnBlankRaiseError = True On Error Goto Trap Min = val(Ini("Limits","Man")) Max = val(Ini("Limits","Mix")) Exit Sub Trap: MsgBox Err.Description & " (" & Err.Number & ")"

IniFileNameDefault static property

Returns or sets the default file name. If a procedure is used without the FileName argument, then IniFileNameDefault is used. If IniFileNameDefault is blank, then it gets set to a name chosen by the system.

How a Default File Name is Chosen

The default file gets the same name as the executable except with the extention INI. The search is something like the Windows Path Search Order.
  1. App.Path
  2. Current Directory
  3. Windows Directory (Skip System Directory)
  4. Directories in the PATH environment variable.
First, it tries to find a file with that name. Write access is preferred. But if one is not found, then the next step depends on the Ini procedure call. If write access is not needed and a file with read access was found, then that path is used. Otherwise, a file creation is attempted using the Search Order. Finally, if nothing works, IniFileNameDefault is left blank which will eventually cause an appropriate error.

IniLabels function

Set colLabels = IniFileLabels(Section$, FileName$) Returns a collection of label names that are in the given section and file name. FileName is optional. If FileName is not filled in, then IniFileNameDefault is used. If the section does not exist or is empty, then the result is set to Nothing.

Example

Dim colLabels As Collection Dim Label As Variant Set colLabels = IniLabels(Section$, FileName$) lstLabels.Clear For Each Label In colLabels lstLabels.AddItem Label Next Label

IniSection function

Set colValues = IniSection(Section$, FileName$) Returns a collection of values that are in the given section and file name. FileName is optional. If FileName is not filled in, then IniFileNameDefault is used. If the section does not exist or is empty, then the result is set to Nothing. This is like IniLabels except that the label names are used as the keys in the collection. Instead of an numeric index, you can use a label name as the key to get a value.

Example

Dim colValues As Collection Dim Label As String Set colValues = IniLabels(Section$, FileName$) Label = InputBox("Enter a label name.") MsgBox "The value of " & Label & " is " colValues(Label)

IniDeleteSection command

Call IniDeleteSection(Section$, FileName$) Deletes the given section including all of its labels and values. FileName is optional. If FileName is not filled in, then IniFileNameDefault is used. There's no way to delete individual labels beyond setting their values to blank.



Feedback Comments
Return Address (if you want a reply)


Copyright 1997 NEWMAN Services Corp. All rights reserved.
973-228-5753 pres@newmanservices.com
DECONSTRUCT THIS PAGE