First, it is necessary to understand the structure of the INI file:
; note
[section name]
Keyword = value
...
The----INI file allows multiple subsections, each of which allows for multiple keywords, followed by the value of the keyword.
There are three types of----values: strings, integer values, and Boolean values. Where the string is stored in the INI file without quotation marks, Boolean truth value is denoted by 1, Boolean false values are expressed in 0.
----comment with a semicolon ";" Beginning.
Second, the definition
----1, increase the inifiles in the uses section of interface;
----2, add a row to the var variable definition section:
Myinifile:tinifile;
----Then, you can create, open, read, write, and so on for the variable myinifile.
Third, open INI file
Myinifile:=tinifile.create (' Program.ini ');
---this line of statements will establish a connection to the variable myinifile to the specific file Program.ini, and then you can read and write the values of the keywords in the Program.ini file through the variable myinifile.
----Note that if the file name in parentheses does not indicate a path, the Program.ini file is stored in the Windows directory, and the Program.ini file is stored in the current directory of the application by specifying the full path and file name for it. The following two statements can complete this function:
Filename:=extractfilepath (paramstr (0)) + ' Program.ini ';
Myinifile:=tinifile.create (filename);
Iv. reading the value of a keyword
The Tinifiles class provides three different object methods to read the values of the keywords in the INI file---The string, Integer numeric, and Boolean three data types that are supported for the INI file.
---assume that the variables defined in VS, vi, and VB are string, Integer, and Boolean types, respectively.
Vs:=myinifile. Readstring (' Bar name ', ' keyword ', default value);
Vi:=myinifile. Readinteger (' Bar name ', ' keyword ', default value);
Vb:=myinifile. Readbool (' Bar name ', ' keyword ', default value);
---where the default value is the default value returned if the INI file does not exist for that keyword.
V. Write INI file
----Similarly, the Tinifile class provides three different object methods that write strings, integers, and Boolean keywords to the INI file.
Myinifile.writestring (' Bar name ', ' keyword ', variable or string value);
Myinifile.writeinteger (' Bar name ', ' keyword ', variable or integer value);
Myinifile.writebool (' Bar name ', ' keyword ', variable or TRUE or false);
----When this INI file does not exist, the above statement will also automatically create the INI file.
Vi. deletion of Keywords
----In addition to adding a keyword to the available write methods, the Tinifile class also provides an object method for deleting the keyword:
Myinifile. DeleteKey (' Bar name ', ' keywords ');
Seven, subsection operation
---Add a section to the method that can be written, delete a section using the following object method:
Myinifile. Erasesection (' subsection name ');
---Another Tinifile class provides three object methods to manipulate the subsections:
---myinifile.readsection (' Bar name ', tstrings variable); All key names in the specified section can be read into a string list variable;
---myinifile.readsections (tstrings variable), you can read all the subsection names in the INI file into a string list variable.
----myinifile.readsectionvalues (' Bar name ', tstrings variable), all rows of the specified section in the INI file (including keywords, =, values) can be read into a string list variable.
Viii. Release
In the appropriate location, release myinifile with the following statement:
Myinifile.distory;
九、一个 instances
----The following is a simple example () that demonstrates the method of building, reading, and storing INI files. The Myini.ini file contains the "program parameters" subsection, and the user name (string), whether the official user (Boolean), and elapsed time (integer value) three keywords. The program reads the data in the form setup and writes the Myini.ini file when the form is released.
---the list of source-attached programs
Unit Unit1;
Interface
Uses
Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs,
Inifiles,//Configuration action file
Stdctrls, Extctrls;
Type
TForm1 = Class (Tform)
Edit1:tedit;
Checkbox1:tcheckbox;
Edit2:tedit;
Label1:tlabel;
Label2:tlabel;
Timer1:ttimer;
Label3:tlabel;
Procedure Formcreate (Sender:tobject);
Procedure Formdestroy (Sender:tobject);
Procedure Timer1timer (Sender:tobject);
Private
{Private declarations}
Public
{Public declarations}
End
Var
Form1:tform1;
Implementation
Var
Myinifile:tinifile;
{$R *. DFM}
Procedure Tform1.formcreate (Sender:tobject);
Var
filename:string;
Begin
Filename:=extractfilepath (paramstr (0)) + ' Myini.ini ';
Myinifile:=tinifile.create (filename);
Edit1. text:= myinifile.readstring (' program parameters ', ' User name ', ' Default user name ');
edit2.text:= IntToStr (Myinifile.readinteger (' program parameters ', ' Elapsed Time ', 0));
CheckBox1. checked:= myinifile.readbool (' program parameters ', ' Whether official user ', False);
End
Procedure Tform1.formdestroy (Sender:tobject);
Begin
Myinifile.writestring (' program parameters ', ' User name ', Edit1. Text);
Myinifile.writeinteger (' program parameters ', ' Elapsed Time ', Strtoint (Edit2.text));
Myinifile.writebool (' program parameters ', ' whether the official user ', CheckBox1. Checked);
Myinifile. Destroy;
End
Procedure Tform1.timer1timer (Sender:tobject);
Begin
Edit2. Text:=inttostr (Strtoint (Edit2.text) +1);
End
End.
===================================================================================
During the development of a database application, the connection between the application side and the database server is an issue that developers must consider. In many cases, developers are programmed to configure the application to connect to the database during design time, package the build installation files after the program completes, and then install them on each client's computer. In this case, both the database server and the database cannot be changed, otherwise the client program cannot connect to the database server, causing the application to be unusable. If you can configure the database connection on each client regardless of how the database server and database changes, you can solve this problem well, and can make the application developed more flexible and have better porting performance.
In the development tools of database applications, Delphi is the tool of choice for many developers, with the advantages of being quick, easy, and easy to debug. Delphi makes it easy to dynamically configure database connections on the application client.
First, it is necessary to briefly describe the structure of the INI configuration file.
INI files generally have several sections, each of which can have several keywords in the form of:
Section
Keyword = value
There are three types of keyword values: string, Integer, and cloth value. In this article, we only use string types. Use ReadString and WriteString in Delphi to read and write (overwrite) string values in the configuration file.
The following is an illustration of how to implement a dynamic metabase connection by using the INI configuration file and the ADO control that comes with Delphi.
This program to implement the function is: The program run, read the database connection configuration file, if the correct connection to the database, the program is normal operation; If the configuration file does not exist or is not correct, you cannot connect to the database, ask whether to reset the configuration file, such as answer "yes", open the Configuration Database Connection window, After the configuration succeeds, the correct configuration is re-written to the configuration file, and the program runs correctly.
First, put the ADOConnection control in the program Data module, and add the Inifiles, adoconed and the Data Module unit to the unit file uses of the Program main window. Declaration of two variables: filename (String type) and Databaseset (Tinifile).
The Activeate event code for the main window is as follows:
Procedure Tfmmain.formactivate (Sender:tobject);
var filename:string;
Databaseset:tinifile;
Begin
Filename:=extractfilepath (paramstr (0)) + ' Databaseset.ini '; Set the path of the configuration file to the executing program directory, and if you do not set the directory, the program will put the configuration file into the Windows system directory.
Databaseset:=tinifile.create (filename);
With Fmdatamodule do//This sentence I did not pass the debugging in the DELPHI6, also do not know what meaning, drew out
Begin
Try//Connect to Database
Adoconnyk.close; Adoconnyk for ADOConnection controls
Adoconnyk.connectionstring:=databaseset. ReadString (' Ykconn ', ' connstr ', '); Reading the database connection configuration string
Adoconnyk.open; Connect to the database as appropriate
Except//configuration file does not exist or is not correct, cannot connect to database
If Application.messagebox (' cannot connect to the database, do you want to reset the connection? ', ' hint ', mb_yesno+mb_iconquestion) =idyes Then
Begin
Adoconnyk.close;
If Editconnectionstring (Adoconnyk) then//Reconfigure connection properties
Begin Databaseset. WriteString (' Ykconn ', ' connstr ', adoconnyk.connectionstring); Overwrite the corresponding keyword value in the configuration file with the correct configuration string.
Adoconnyk.open; Connect the database after you have configured it correctly
End
End
Else
Begin
ShowMessage (' Database connection settings are not correct, please contact the administrator before using the system ');
Application.terminate;
End
End
Freeandnil (Databaseset); Release the resources for the configuration file
End
End
Create a database configuration file ini (GO)