
Remove a Key From an INI File
by Irina Segalevich
June 2003 Issue
Technology Toolbox: VB6, VB.NET, C#, ASP.NET
Level: Intermediate
Windows initialization (INI) files are often used to store and retrieve optional settings that applications might need at run time. Common examples of INI files are WIN.INI, SYSTEM.INI, and SHARED.INI. INI files are text-based configuration files, so you can use any text editor to change an application's settings from outside the application. Windows provides special API functions to read and write information in the INI files. The most commonly used functions are GetPrivateProfileString and WritePrivateProfileString. However, there is no function to delete information from an INI file. I'll show you how to delete a key or its value from an INI file.
An INI file has a standard structure. The file's main elements are the section name and the key name. An INI file contains one or more sections. Each section has a section name enclosed in square brackets ([ ]). Each section could have one or more keys with unique names and values. A key identifies a setting and its value. Key name and value are separated by an equal sign (=):
Key1=key1value
This INI file contains two sections:
[Section1]
key1=key1value
key2=key2value
[Section2]
key1=value1
key2=value2
key3=value3
Section 1 has two keys with the values key1value and key2value. Section 2 has three keys with the values value1, value2, and value3.
Two Windows API functions—WritePrivateProfileString and GetPrivateProfileString—write information to and read information from a private INI file. You must include both function declarations in the Visual Basic application. You use the WritePrivateProfileString function to write or modify a value in an INI file:
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
In the preceding code, lpApplicationName is the section name. If a section name exists, function WritePrivateProfileString uses the existing section. lpKeyName is the name of the key; lpString is the value of the key; and lpFileName is the name of the INI file (the path is optional).
Back to top
|