An INI file is a structured text file that stores settings for user-configurable program features. For example, an INI file is one mechanism for remembering that a user wants text to display in a language other than English. The original Windows Application Programmer Interface (API) supported functions that facilitate reading and writing INI files. Microsoft has deprecated INI files in favour of the Windows Registry and newer XML-based file formats. The Windows API functions, while not directly supported, remain available to Visual Basic 9 programmers who support legacy software.
- Skill level:
Other People Are Reading
Add a Declare statement to the top of your module to enable calling the Windows API function for writing .ini files:
Private Declare Ansi Function WritePrivateProfileString _
Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _ (ByVal lpApplicationName As String, _ ByVal lpKeyName As String, ByVal lpString As String, _ ByVal lpFileName As String) As Integer
Add a Declare function at the top of your module to enable calling the Windows API function for reading .ini file value:
Private Declare Ansi Function GetPrivateProfileString _
Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, _ ByVal lpKeyName As String, ByVal lpDefault As String, _ ByVal lpReturnedString As String, _ ByVal nSize As Integer, ByVal lpFileName As String) _ As Integer
Create or edit an INI file by calling the WritePrivateProfileString function, providing values for key, application and filename. By convention, INI files use the .ini extension. The application parameter corresponds to a section in the ini file. If any of the specified parameters do not exist, they will be created. This includes the file itself as long as the specified directory already exists. The WritePrivateProfile String function will overwrite previously existing values.
WritePrivateProfileString("section", "key", "value", "./data.ini")
Flush the cache when you have completed INI file modifications, reducing the risk of data loss in the event of an abnormal program shutdown. Pass zero values to the WritePrivateProfileString function for all of the parameters except the INI file name. This instructs Windows to write changes to the disk:
WritePrivateProfileString(0, 0, 0, "./data.ini")
Allocate a buffer sufficient to hold the maximum size of a value stored in the INI file. A buffer of 256 characters is usually sufficient. Create an integer to receive the actual size of the value that was retrieved from the INI file. Use Dim to declare the variables and the Space function to allocate memory for the string:
Dim strValue As String
Dim intLen As Integer
strValue = Space(256)
Retrieve values stored in the INI file by calling GetPrivateProfileString using the variables along with key, section, and file name values used when writing the INI file. Specify the size of the buffer that you are providing and a value to be returned when the designated value is not found:
intLen = GetPrivateProfileString("section", "key", "not found", strValue, 256, "filename")
Tips and warnings
- Visual Basic 9, released in late 2007 along with .NET version 3.5, is also known as Visual Basic 2008. Version 6 was the final version of Visual Basic released prior to its integration .NET framework.
- The newer XML formats provide an easy to use and more powerful means of storing program configuration data in web.config, Machine.config and Application.config files.
- You can use supported standard file IO operations to read and write INI files. This will require that you implement the parsing logic to navigate sections and keys within the text files.
- Using older Windows API functions to bypass the .NET framework bypasses "managed code." Allowing the system to automatically manage resources requires less programming to achieve more reliable software with fewer errors.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for