How to use the VB development application how to use INI files
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 = ValuelKeyword2 = Value2 ... [section2] Keyword1 = value1keyword2 = value2 ......
Among them, if there is no content (ie, value is empty), the Windows application has specified the default value for this keyword. If a keyword is not found in the entire file (or the whole part) It also expressed the default value to them. 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.
The rwini.ba module program code is as follows:
The WindowsAPI function used in the General-Decleration section:
DeclareFunctionGetprivateprofileStringLib "Kernel" (ByVallpAppNameAsString, ByVallpKeyNameAsString, ByVallpDefaultAsString, ByVallpRetrm-StringAsString, ByValcbReturnStringAsInteger, ByValFilenameAsString) AsIntegerDeclareFunctionGetPrivatePfileIntLib "Kernel" (ByVallpAppNameAsString, ByVallpKeyNameAsString, ByVallpDefaultAsInteger, ByValFilenameAsString) AsIntegerDeclareFuncitonWritePrivateprofileStringLib "Kernel" (ByVallpApplicationNameAsString, ByVallpKeyNameAsString, ByVallpStringAsString, ByVallplFileNameAsString) AsIntegerFunctionGetIniS (ByValSectionNameAsString , ByValKeyWordAsString, ByValDefStringAsString) AsStringDimResultStringAsString * 144, TempAsIntegerDimsAsString, iAsIntegerTemp% = GetPrivateProfileString (SectionName, keyWord, "", ResultString, 144, AppProfileName ()) 'search keyword value IfTemp%> 0Then' not empty keyword s = "" Fori = 1To144IfAsc (Mid $ (ResultString, I, 1)) = 0ThenExitForElses = s & Mid $ (ResultString, I, 1) EndIfNextElseTemp% = WritePrivateProfilesString (sectionname, KeyWord, DefString, ppProfileName ()) 'the default value written INI file s = Defstringendifget IniS = sEndFunctionFunctionGetIniN (ByValSectionNameAsString, ByValKeyWordAsString, ByValDefValueAsIneger) AsIntegerDimdAsLong, sAsStringd = DefValueGetIniN = GetPrivateProfileInt (SectionName, KeyWord, DefValue, ppProfileName ()) Ifd <> DefValueThens = "" & dd = WritePrivateProfileString (SectionName, KeyWord, s, AppProfileName ()) EndIfEndFunctionSubSetIniS (ByvalsectionNameAMeasstring, BtvakeywordAsstring,
ByValValStrAsString) Dimres% res% = WritePrivateprofileString (SectionName, KeyWord, ValStr, AppProfileName ()) EndSubSubSetIniN (ByValSectionNameAsString, ByValKeyWordAsString, ByValValIntAsInteger) Dimres%, s $ s $ = Str $ (ValInt) res% = WriteprivateProfileString (SectionName, KeyWord, s $, Appprofilename ()) endsubsectionname For each section, keyword is keyword, getinis and defvalue in GetInis and Getinin for the default value of the keyword, setinis and setinin Valstr and Valint For the value of keywords to write into II files . In order to better explain how to use the above functions and processes, two instances will be held below.
Example 1:
Developing applications typically use databases and other files, directory (including paths and file names) of these files should not be fixed in the program, but are saved in the INI file, and the program is run by the INI file. The code to read the database file is as follows:
DimdatabaseName = GetInis ("Database", "Workers", "") ifdatabaseName = "" "" "" ", please enter the database" Workers "directory"), app.title) 'can also choose from "File dialog box" OneRrorResuMenextSetDB = OpenDataBas (DatabaseName) Iferr <> 0THENMSGBOX "Open Database Failed!", MB-iconStop, App.Title: GotoErrorProcessingeLsesetinis "Database", "Workers", DatabaseNameEndifonerrorgoto0 ......
Example 2:
To facilitate user operation, some information of the user interface is sometimes necessary, such as the height and width of the window. When loading the form, read the form height and width from the INI file, and store the form current height and width when the form is uninstalling the form, 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.heightsetinin "Form 1," Width ", Me.Width ... End Sub