In order to facilitate user use and make the system flexible, most Win-dows applications record the choices made by the user and various changed system information in the initialization (INI) file. Therefore, when the system environment changes, the INI file can be modified directly without modifying the program. It can be seen that the INI file is crucial to system functionality. This article will introduce how to read and write INI files when developing Windows applications using Visual Basic for Windows (hereinafter referred to as VB).
The INI file is a text file that consists of several sections. Under each bracketed title, there are several keywords (keyWord) starting with a single word and an equal sign. Each keyword controls a certain aspect of the application. The working mode of each function, the value on the right side of the equal sign specifies the operating mode of the keyword. Its general form is as follows:
[section1]
keyword1=value
keyword2=value2
…
[section2]
keyword1=value1
keyword2=value2
…
Among them, if there is nothing on the right side of the equal sign (that is, the value is empty), it means that the Windows application has specified a default value for the keyword. If a keyword (or an entire part) is not found in the entire file , which also means that default values are specified for them. The order in which the parts appear is irrelevant, as is the order of the keywords within each part.
There are usually two ways to read and write INI files: one is to edit it with "Notepad" in Windows, which is relatively simple and needs no further description; the other is to read and write INI files by a Windows application, usually by running the application The information in the INI file is read when the application is exited, and certain modifications made by the user to the running environment are saved when the application is exited.
The value type of keywords is mostly string or integer, and should be read and written in two situations. In order to make the program maintainable and portable, it is best to encapsulate the reading and writing of INI files in a module (RWINI.BAS), and construct the GetIniS and GetIniN functions as well as SetIniS and SetIniN in RWI-NI.BAS. Process, you need to use the "GetPRivateprofileString", "GetPrivateProfileInt" and "WritePrivateProfileString" functions of Windows API in these functions and processes.
The program code of the RWINI.BAS module is as follows:
Declare the Windows API functions used in the General-Declearation section:
DeclareFunctionGetprivateprofileStringLib"Ker-nel"(ByVallpAppNameAsString, ByVallpKeyNameAsString, ByVallpDefaultAsString, ByVallpRetrm-StringAsString, ByValcbReturnStringAsInteger, ByValFilenameAsString)AsInteger
DeclareFunctionGetPrivatePfileIntLib"Kernel"(ByVallpAppNameAsString, ByVallpKeyNameAsString, ByVallpDefaultAsInteger, ByValFilenameAsString)AsInteger
DeclareFuncitonWritePrivateprofileStringLib"Kernel"(ByVallpapplicationNameAsString, ByVallpKeyNameAsString, ByVallpStringAsString, ByVallplFileNameAsString)AsInteger
FunctionGetIniS(ByValSectionNameAsString, ByValKeyWordAsString, ByValDefStringAsString)AsString
DimResultStringAsString*144,TempAsInteger
DimsAsString, iAsInteger
Temp=GetPrivateProfileString(SectionName,KeyWord,"",ResultString,144,AppProfileName())
'Retrieve the value of the keyword
IfTemp>0Then'keyword value is not empty
s=""
Fori=1To144
IfAsc(Mid$(ResultString,I,1))=0Then
ExitFor
Else
s=s&Mid$(ResultString, I, 1)
EndIf
Next
Else
Temp=WritePrivateProfilesString(sectionname,KeyWord,DefString,ppProfileName())
'Write default values to INI file
s=DefString
EndIf
GetIniS=s
EndFunction
FunctionGetIniN(ByValSectionNameAsString, ByValKeyWordAsString, ByValDefValue
AsIneger)AsInteger
DimdAsLong,sAsString
d=DefValue
GetIniN=GetPrivateProfileInt(SectionName,
KeyWord,DefValue,ppProfileName())
Ifd<>DefValueThen
s=""&d
d=WritePrivateProfileString(SectionName,
KeyWord,s,AppProfileName())
EndIf
EndFunction
SubSetIniS(ByValSectionNameAsString, BtVaKeyWordAsString, ByValValStr
AsString)
Dimres
res=WritePrivateprofileString(SectionName,KeyWord,ValStr,AppProfileName())
EndSub
SubSetIniN(ByValSectionNameAsString, ByValKeyWordAsString, ByValValInt
AsInteger)
Dimres,s$
s$=Str$(ValInt)
res=WriteprivateProfileString(SectionName,KeyWord,s$,AppProfileName())
EndSub
SectionName is the title of each section, KeyWord is the keyword, DefValue in GetIniS and GetIniN is the default value of the keyword, and ValStr and ValInt in SetIniS and SetIniN are the values of the keyword to be written to the INI file. In order to better illustrate how to use the above functions and procedures, two examples are given below.
Example 1:
Developing applications usually requires the use of databases and other files. The directories of these files (including paths and file names) should not be fixed in the program, but are saved in INI files, which are read from the INI files when the program is running. The code to read the database file is as follows:
DimDatabasenameAsString
Databasename=GetIniS("database", "employee", "")
IfDatabaseName=""ThenDatabaseName=InputBox("Please enter the directory of the database "Employee"),
App.Title)' can also be selected through the "File Dialog"
OnErrorResumeNext
Setdb=OpenDatabas(DatabaseName)
IfErr<>0Then
MsgBox "Failed to open database!", MB-
ICONSTOP,App.Title:GotoErrorProcessing
Else
SetIniS"Database","Employee",DatabaseName
EndIf
OnErrorGoTo0
…
Example 2:
In order to facilitate user operations, sometimes it is necessary to save certain information of the user interface, such as the height and width of the window. When loading the form, read the height and width of the form from the INI file. When unloading the form, store the current height and width of the form in the INI file. The code is as follows:
SubForm1_Load()
…
Forml.Height=GetIniN("Form 1", "Height", 6000)
Form1.Width=GetIniN("Form1","Height",4500)
EndSub
…
SubForm1_Unload()
…
SetIniN"Form1","Height",Me.Height
SetIniN"Form1,"Width",Me.Width
…
EndSub->