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).

If the FileName you specify in lpFileName exists already, then the function WritePrivateProfileString modifies the existing file. If the file doesn't exist, the function creates a new file. If you don't specify the path in lpFileName, then WritePrivateProfileString searches the Windows directory. If the key name doesn't exist already, then the function adds it under the specified section name.

You use the GetPrivateProfileString function to access information from an INI file:

Declare Function GetPrivateProfileString Lib _
   "kernel32 " Alias "GetPrivateProfileStringA" _
   (ByVal lpApplicationName as String, _
   ByVal lpKeyName as Any, _
   ByVal lpDefault as String, _
   ByVal lpReturnedString as String, _
   ByVal nSize as Long, _
   ByVal lpFileName as String) as Long

In the preceding code, lpApplicationName is the section name; lpKeyName is the key name; lpDefault is the default value if a key doesn't exist; lpReturnedString is the returned value; nSize is the size of the buffer for the value to be put into; and lpFileName is the name of the private INI file.

There are functions for reading and writing information from an INI file, but Windows doesn't provide a direct function to delete information from an INI file. You can achieve this by using the WritePrivateProfileString function and passing an empty string for the lpString parameter.

Here's an example of how to delete a key from the previous INI file:

'this goes into the declaration section of the 
'code
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

Public sub DeleteKey
Dim sSection As String
Dim sKey As String
Dim sFileName as String

sSection = "Section1"
sKey = "Key1"
sFileName="C:\MyINIFILE.INI"

'if the key exists, delete it from the section
   If len(trim(sKey)) <>0 Then
      WritePrivateProfilestring sSection, sKey, _
         vbNullString, sFileName
   Else
      'no key specified, then delete section
      WritePrivateProfileString _
         sSection,sKey,vbNullString,sFileName
   End If
End Sub

The preceding code shows how to use the WritePrivateProfileString function with a null key value to delete the keys from any INI file. This subprocedure's code checks first if the key is specified. If the key exists, it's deleted. If the key isn't specified, the function deletes the whole section.

About the Author
Irina Segalevich is a software developer at Financial Processing Systems Inc., located in Santa Ana, Calif. She specializes in VB, SQL Server, and Crystal Reports. Reach her at .