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: Declarefunctiongetprivateprofilestringlib "Ker-nel" (byvallpappnameasstring, Byvallpkeynameasstring, byvallpdefaultasstring, byvallpretrm-stringas String, byvalcbreturnstringasinteger, byvalfilenameasstring) asinteger Declarefunctiongetprivatepfileintlib "kernel" (byvallpappnameasstring, Byvallpkeynameasstring, byvallpdefaultasinteger, byvalfilenameasstring) Integer Declarefuncitonwriteprivateprofilestringlib "kernel" (byvallpapplicationname Asstring, byvallpkeynameasstring, byvallpstringasstring, byvallplfilename Asstring) asinteger Functiongetinis (byvalsectionnameasstring, byvalkeywordasstring, byvaldefstring Asstring) asstring Dimresultstringasstring * 144, tempasinteger Dimsasstring, iasinteger Temp % = getprivateprofilestring (sectionname, keyword, "", resultstring, 144, appprofilename ()) 'Retrieve the keyword Value The value of the iftemp %> 0then' keyword is not empty. S = "" Fori = 1to144 Ifasc (mid $ (resultstring, I, 1) = 0 then Exitfor Else S = S & Mid $ (resultstring, I, 1) Endif Next Else Temp % = writeprivateprofilesstring (sectionname, keyword, defstring, ppprofilename ()) 'Write the default value to the 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 <> defvaluettings 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 part. keyword is the keyword. defvalue in getinis and getinin is the default value of the keyword. valstr and valint of setinis and setinin are the values of the keywords to be written into the INI file. To better illustrate how to use the above functions and procedures, the following two instances are provided. Instance 1: Databases and other files are usually used for application development. The directories (including paths and file names) of these files should not be fixed in the program, but saved in the INI file, when the program is running, it is read in the INI file. The code for reading database files is as follows: Dimdatabasenameasstring Databasename = getinis ("Database", "employee", "") Ifdatabasename = "" thendatabasename = inputbox ("Enter the Database" employee "directory "), App. Title) 'can also be selected through the "file dialog box" Onerrorresumenext Setdb = opendatabas (databasename) Iferr <> 0 then Msgbox "failed to open database !", MB- Iconstop, app. Title: gotoerrorprocessing Else Setinis "Database", "employee", databasename Endif Onerrorgoto0 ...... Instance 2: To facilitate user operations, you may need to save some information on the user interface, such as the window height and width. When loading a form, read the form height and width from the INI file. When uninstalling the form, save the current height and width of the form to the INI file. The Code is as follows: Sub form1_load () ...... Forml. Height = getinin ("Form 1", "height", 6000) Form1.width = getinin ("Form 1", "height", 4500) Endsub ...... Sub form1_unload () ...... Setinin "Form 1", "height", me. Height Setinin "Form 1," width ", me. Width ...... End sub |