To make it easier for users to use and make the system flexible, most Win-dows applications record user choices and various changes in the system information in the initialization (INI) file. Therefore, when the system environment changes, you can directly modify the INI file without modifying the program. It can be seen that the INI file is crucial to system functions. This article describes how to read and write INI files when using Visual basicforwindows (VB) to develop Windows applications.
An INI file is a text file consisting of several sections. under each heading with parentheses, It is a number of keywords that begin with a single word (keyword) and an equal sign, each keyword controls how a function of an application works. The value on the right of the equal sign specifies the keyword operation method. The general form is as follows:
[Section1]
KeyWord1 = Valuel
KeyWord2 = Value2
......
[Section2]
KeyWord1 = Value1
KeyWord2 = Value2
......
If there is no content on the right of the equal sign (that is, the Value is null), it means that the Windows application has specified the default Value for this keyword, if a keyword (or entire part) cannot be found in the entire file, it also indicates that the default value is specified for them. The order in which each part appears is irrelevant. In each part, the order of each keyword is also irrelevant.
There are two ways to read and write INI files: one is to use Notepad in Windows to edit it, which is relatively simple and does not need to be described in detail; the other is to read and write INI files by Windows applications, normally, the application reads information in the INI file when running the program, and saves some modifications to the running environment when exiting the application.
The Value Type of a keyword is string or integer. You can read and write the keyword in two cases. In order to make the program maintainability and portability, it is best to encapsulate the INI file read and write in a module (RWINI. in BAS), construct the GetIniS and GetIniN functions in the RWI-NI.BAS and the SetIniS and Se-tIniN processes, in these functions and procedures, you must use the "GetPrivateprofileString", "GetPrivateProfileInt", and "WritePrivateProfileString" functions of WindowsAPI.
The program code of the RWINI. BAS module is as follows:
Declare the WindowsAPI function used in the General-Declearation section: