CT INIFile ActiveX Library allows you to read from and write to initialization/INI files from Visual Basic applications. This library file is specifically designed for Visual Basic, but may work in any environment that supports ActiveX technology.
Classes |
---|
INIFileLib Class |
|||
---|---|---|---|
Properties |
Methods |
Events |
|
|
|||
|
|
||
|
|
An initialization file (INI extension) is a simple text file especially formatted to pass various settings to a program. An INI file can easily be created using Notepad, Wordpad, or any other word processor that saves in plain text format. The proper format for an initialization file looks like this one:
[Section]
Key_1=Value_1
Key_2=Value_2
Key_3=Value_3
You are not limited to the number of sections or keys entries that you can put in an initialization file. You can have as few or as many as you need.
This class allows you to easily read from and write to an initialization file. Properties and methods of this class can be invoked as if they were, without the need for explicitly creating or referencing an object. However, you are still free to create as many objects as you wish by declaring them As New.
The INIFile property returns/sets the path and filename of a particular intialization file.
INIFile As String
When you create an instance of this class, this property is set to
a zero-length string (""). Always set an initial value for
this property before reading from or writing to a different file (in
advance of calling methods in this class). This greatly simplifies
the task of repeatedly access to the same file. The value of this
property is ignored when you pass a specific initialization file to
methods of this class.
If this property contains the file name
only, the system searches for the file in the Windows folder by
default. If no file is specified, all of the methods exposed by this
class will failed. This property setting can be a qualified network
path and filename using the following syntax:
\\servername\sharename\pathname.
Do not use wildcard
characters.
INIFile = "c:\My Documents\My Application.ini"
The INIQuerySectionNames function retrieves the names of all sections in an initialization file.
Function INIQuerySectionNames(SectionNames() As String, Optional ByVal Size As Long = 2540, Optional ByVal File As String) As Long
SectionNames |
An array containing the section names. |
Size |
The maximum size of the array, in characters. |
File |
A specific initialization file. |
The function returns the number of array elements (sections).
None
Dim INIObj As New
INIFileLib
Dim Sections() As String
Dim ArrCount As Long
Dim
Num As Long
INIObj.INIFile = "c:\My Documents\My Application.ini"
ArrCount = INIObj.INIQuerySectionNames(Sections)
For Num = 0 To ArrCount -
1
Debug.Print Sections(Num)
Next
Erase Sections
Set INIObj
= Nothing
The INIQuerySettingNames function retrieves all of the keys for the specified section from an initialization file.
Function INIQuerySettingNames(ByVal Section As String, KeyNames() As String, Optional ByVal Size As Long = 2540, Optional ByVal File As String) As Long
Section |
The section name from which will be retrieved the keys. |
KeyNames |
An array containing the key names under the Section. |
Size |
The maximum size of the array, in characters. |
File |
A specific initialization file. |
The function returns the number of array elements (keys).
None
Dim INIObj As New
INIFileLib
Dim Keys() As String
Dim ArrCount As Long
Dim Num
As Long
INIObj.INIFile = "c:\My Documents\My Application.ini"
ArrCount = INIObj.INIQuerySettingNames("MySection", Keys)
For Num = 0 To ArrCount -
1
Debug.Print Keys(Num)
Next
Erase Keys
Set INIObj =
Nothing
The INIQuerySetting function retrieves the value corresponding to a key from the specified section in an initialization file.
Function INIQuerySetting(ByVal Section As String, ByVal KeyName As String, Value, Optional ByVal Size As Long = 254, Optional ByVal File As String) As Boolean
Section |
The section name where KeyName is located. |
KeyName |
The key name you are interested in getting the value for. |
Value |
The value associated to KeyName. |
Size |
The maximum size of the Value, in characters. |
File |
A specific initialization file. |
If the function succeeds, the return value is True. If the function fails, the return value is False, that is when the returned string (Value) is the same as the original one (Value) passed to the function. Set the Value to something that would definitely not be read, such as "error!".
This method supports the following data types for the value associated to the specified key name:
Data Types |
Storage Size |
Range |
Byte |
1 byte |
0 to 255 |
Integer |
2 bytes |
-32,768 to 32,767 |
Long integer |
4 bytes |
-2,147,483,648 to 2,147,483,647 |
Single-precision floating point number |
4 bytes |
-3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values |
Double-precision floating point number |
8 bytes |
-1.79769313486232E308 to |
Currency (scaled integer) |
8 bytes |
-922,337,203,685,477.5808 to 922,337,203,685,477.5807 |
Date |
8 bytes |
January 1, 100 to December 31, 9999 |
String |
10 bytes + string length |
0 to approximately 2 billion |
Boolean |
2 bytes |
True or False |
Dim INIObj As New
INIFileLib
Dim Value As String
INIObj.INIFile = "c:\My Documents\My Application.ini"
INIObj.INIQuerySetting
"MySection", "MyKey", Value
Debug.Print
"MyKey=" & Value
Set INIObj = Nothing
The INIWriteSetting function replaces the key and value under the specified section in an initialization file.
Function INIWriteSetting(ByVal Section As String, ByVal KeyName As String, ByVal Value As String, Optional ByVal File As String) As Boolean
Section |
The section name where KeyName is located. |
KeyName |
The key name you are interested in writing the value for. |
Value |
The value associated to KeyName. |
File |
A specific initialization file. |
If the function succeeds, the return value is True. If the function fails, the return value is False.
If the initialization file you try to write to does not exist, it will be created. Likewise, if the Section or KeyName does not exist, it will also be created.
Dim INIObj As New INIFileLib
INIObj.INIFile = "c:\My Documents\My Application.ini"
INIObj.INIWriteSetting "MySection", "MyKey", "MyValue"
Set INIObj = Nothing
The INIClearSection function deletes all of the settings from and including the name of the specified section in an initialization file.
Function INIClearSection(ByVal Section As String, Optional ByVal File As String) As Boolean
Section |
The section name intended to be removed. |
File |
A specific initialization file. |
If the function succeeds, the return value is True. If the function fails, the return value is False.
None
Dim INIObj As New INIFileLib
INIObj.INIFile = "c:\My Documents\My Application.ini"
INIObj.INIClearSection "MySection"
Set INIObj = Nothing
The INIClearSetting function deletes the key and the value under the specified section in an initialization file.
Function INIClearSetting(ByVal Section As String, ByVal KeyName As String, Optional ByVal File As String) As Boolean
Section |
The section name where KeyName is located. |
KeyName |
The key name intended to be removed along with its value. |
File |
A specific initialization file. |
If the function succeeds, the return value is True. If the function fails, the return value is False.
None
Dim INIObj As New INIFileLib
INIObj.INIFile = "c:\My Documents\My Application.ini"
INIObj.INIClearSetting "MySection", "MyKey"
Set INIObj = Nothing
Windows 95/98, NT, 2000
Visual Basic 6.0 RunTime
files
You can get Visual Basic 6.0 RunTime files from
Simtel.Net
http://www.simtel.net/pub/simtelnet/win95/dll.html
Reference for further information
http://softrunner.homestead.com
Comments, suggestions or bugs reports
cbotez@homestead.com
CT INIFile ActiveX Library
Copyright ©
1999-2000, Cezar Botez
All rights reserved