VB how to use the INI file

Source: Internet
Author: User
Tags ini integer
For the convenience of users and flexibility of the system, most win-dows applications record user choices and system information for various changes in the initialization (INI) file. Therefore, when the system environment changes, you can directly modify the INI file, without modifying the program. This shows that the INI file is critical to the system function. This article will introduce how to read and write INI files when using Visualbasicforwindows (hereinafter called VB) to develop Windows applications.
The INI file is a text file, consisting of several sections (section), below each bracketed heading, a number of keywords that begin with a single word (keyword), and an equal sign, each of which controls how a function of the application works, the value on the right side of the equal sign (value) Specify how keywords are operated. The general form is as follows:

[Section1]
Keyword1=valuel
Keyword2=value2
......
[Section2]
Keyword1=value1
Keyword2=value2
......

Where there is nothing on the right side of the equal sign (that is, value is empty), that means that the Windows application has specified a default value for the keyword, and if a keyword (or an entire part) is not found throughout the file, it also indicates that the default value is specified for them. The order in which the parts appear is irrelevant, and the order of each keyword in each section is equally irrelevant.

There are usually two ways to read and write an INI file: One is to edit it with Notepad in Windows, which is simpler, needless to repeat; the second is to read and write the INI file by the Windows application, usually the information in the INI file when the application is run. Saves some of the user's changes to the running environment when exiting the application.

The value of a keyword is more or more of a string or integer type, and should be read and written in two cases. In order to make the program maintainable and portable, it is best to encapsulate the read and write of the INI file in a module (Rwini). BAS), the Getinis and Getinin functions and the Setinis and Se-tinin processes are constructed in Rwi-ni.bas, which require the use of WindowsAPI's "getprivateprofilestring", " Getprivateprofileint "and" writeprivateprofilestring "functions.

Rwini. The program code for the BAS module is as follows:

Declare the WINDOWSAPI function that is used in the General-declearation section:

Public Declare Function getprivateprofilestring Lib "Kernel" (ByVal lpappname as String,byval Lpkeyname as _
String,byval Lpdefault as String,byval lpretrm-string as String,byval cbreturnstring as Integer, _
ByVal Filename as String) as Integer
Public Declare Function getprivatepfileint Lib "Kernel" (ByVal lpappname as String,byval lpkeyname as String, _
ByVal Lpdefault as Integer,byval Filename as String) as Integer
Public Declare Funciton writeprivateprofilestring Lib "Kernel" (ByVal Lpapplicationname as String, _
ByVal Lpkeyname as String,byval lpstring as String,byval lplfilename as String) as Integer
Public Function Getinis (ByVal sectionname as string,byvalkeyword as String,byval defstring as String) as String
Dim resultstring as string*144,temp as Integer
Dim s as string,i as Integer
Temp%=getprivateprofilestring (Sectionname,keyword, "", Resultstring,144,appprofilename ())
' Retrieve the value of the keyword
The value of the If temp%>0 Then ' keyword is not null
S= ""
For I=1 to 144
If ASC (mid$ (resultstring,i,1)) =0 Then
Exitfor
Else
S=s & mid$ (resultstring,i,1)
End If
Next
Else
Temp%=writeprivateprofilesstring (Sectionname,keyword,defstring,ppprofilename ())
' Write default value to INI file
S=defstring
End If
Getinis=s
End Function

Public function getinin (Byval sectionname as string,byval keyword as  string,byval defvalue as ineger) As integer
Dim d as long,s as  string
D=defvalue
Getinin=getprivateprofileint (sectionname,
Keyword,defvalue,ppprofilename ())
If d <> defvalue then
s= "
d=writeprivateprofilestring (sectionname,
Keyword,s,appprofilen Ame ())
End if
End function

Public Sub Setinis (ByVal sectionname as String,btval KeyWord as String,byval valstr as String)
Dim res%
Res%=writeprivateprofilestring (Sectionname,keyword,valstr,appprofilename ())
End Sub
Public Sub Setinin (ByVal sectionname as String,byval KeyWord as string,byval as Integer)
Dim res%,s$
s$=str$ (Valint)
Res%=writeprivateprofilestring (Sectionname,keyword,s$,appprofilename ())
End Sub

SectionName for each part of the title, keyword for keywords, getinis and getinin defvalue for the keyword defaults, setinis and Setinin Valstr and valint for the value of the keywords to be written to the INI file. To better illustrate how to use the above functions and procedures, here are two examples.

Example 1:

Development applications typically use databases and other files, directories (including paths and filenames) that should not be fixed in the program, but stored in the INI file, which is read in the INI file when the program runs. The code to read into the database file is as follows:

Dim Databasename as String
Databasename=getinis ("Database", "Staff", "")
If databasename= "" Then databasename=inputbox ("Please enter the directory of the Worker"), App.title)
' can also be selected through the File dialog box '
On Error Resume Next
Set Db=opendatabas (DatabaseName)
If Err <> 0 Then
MsgBox "Open database failed!", mb-iconstop,app.title:gotoerrorprocessing
Else
Set Inis "Database", "Staff", DatabaseName
End If
On Error GoTo 0
......

Example 2:

Some information about the user interface, such as the height and width of the window, is sometimes needed to facilitate user action. When the form is loaded, the form's height and width are read from the INI file, and the current height and width of the form are saved to the INI file when the form is unloaded, as follows:

Private Sub Form1_Load ()
......
Forml.height=getinin ("Form 1", "height", 6000)
Form1.width=getinin ("Form 1", "height", 4500)
Endsub
......
Private Sub Form1_unload ()
......
Setinin "Form 1", "height", me.height
Setinin "Form 1," width ", me.width
......
End Sub



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.