How to use the VB development application how to use INI files
Author: Evaluation: the station Date: 2001-09-04 Description: Source:
In order to facilitate user use and make the system flexibly, most Win-DOWS applications record the selection of users and various changes in the initialization (INI) file. Therefore, when the environment of the system changes, INI files can be directly modified without modifying the program. It can be seen that the INI file is critical to system functions. This article will introduce how to read and write the INI file when developing a Windows application using VisualBasicForWindows (hereinafter referred to). The INI file is a text file, which consists of several sections. Here's a header of each with a bracket, is a keyword that starts with a single word (Keyword) and one equal sign, each keyword will control the application Features work mode, the value of the right side (Value) specifies the operation mode of the keyword. The general form is as follows: [section1] keyword1 = Valuel keyword2 = value2 ... [section2] keyword1 = value1 keyword2 = value2 ... where there is no content (ie, the value is empty), then the Windows application has The default value is specified for this keyword, and if a keyword (or a whole) is not found throughout the file, it also indicates that the default value is specified. The order in which each part appears is irrelevant. In each portion, the order of each keyword is also significant. Reading and writing Ini files There are usually two ways: First, edit it with "Notepad" in Windows, it is relatively simple, and it is not necessary to read and write the INI file by the Windows application, usually the application run. Read the information in the INI file, save some modifications to the running environment when you exit the application. The type of keyword is mostly a string or integer type, and should be read and written in two cases. In order to make the program have maintainability and portability, it is best to make read and write packages on the INI file in a module (RWINI.BAS), construct Getinis and Getinin functions in rwi-ni.bas, and setinis and se-Tinin Procedure, in these functions and processes, you need to use WindowsAPI's "getPrivateProfileString", "getPrivateProfileint" and "WritePrivateProfileString" functions.
RWINI.BAS program code means as follows: General-Declearation section statement used to WindowsAPI function: Declare Function GetprivateprofileString Lib "Ker-nel" (ByVallpAppName As String, ByVallpKeyName As String, ByVallpDefault As String, ByVal lpRetrm-String As String , ByVal cbReturnString As Integer, ByVal Filename As String) As Integer Declare FunctionGetPrivatePfileInt Lib "Kernel" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As Integer, ByVal Filename As String) As Integer Declare FuncitonWritePrivateprofileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lplFileName As String) As Integer Function GetIniS (ByVal SectionName As String, ByVal KeyWord As String, ByVal DefString As String) As String Dim ResultString As String * 144, Temp As Integer DIMS AS STRING, I AS Integer Temp% = getPrivateProfileString (SectionName, Keyword, "", ResultString, 144, Appprofilename ()) The value of the value iftemp%> 0then 'keyword is not empty s = "" Fori = 1to144 IFASC (RESULTSTRING, I, 1)) = 0Then ExitFor Else s = s & Mid $ (ResultString, I, 1) EndIf Next Else Temp% = WritePrivateProfilesString (sectionname, KeyWord, DefString, ppProfileName ()) 'writes INI file Default s = DefString EndIf GetIniS = s EndFunction FunctionGetIniN (BYVALKEYWORDASSTRING, 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 for each portion Title, keyword is keyword, the defverue in Getinis and Getinin is the default value of the keyword, the Valstr and Valint of Setinis and Setinin are the value of the keyword to write to the INI file. In order to better explain how to use the above functions and processes, two instances will be held below. Example 1: Developing applications typically uses databases and other files, directory (including path and file name) of these files, should not be fixed in the program, but save in the INI file, the program is running by the ini file. . The code read into the database file is as follows: DimdatabaseNameAsstring DatabaseName = Getinis ("Database", "Workers", ") ifdatabaseName =" "" "", please enter the database "of the catalog") You can select OneRrorResuMenext setDb = OpenDatabaseUmenext setdb = OpenDatabas (DatabaseName) Iferr <> 0then msgbox ", MB- iconstop, app.title: gotoerrorprocessing else setinis" database "," employee ", DatabaseName EndifOn0 ... Example 2: In order to facilitate user operation, sometimes some information of the user interface, such as the height and width of the window, and the like.