Use delphi in the configuration file (* .ini) and TIniFile usage

A configuration file 

.ini file is a file format based on the type of text, for storing programs and configuration data to initialize. 

.ini file segment is (Sections) and key (key), each of which may have a file n segments (each segment enclosed in square brackets), each segment can have keys m, substantially in the following format: 

    [ secontion] 

     KeyName1 = Value1; 

     ; Comment 

     KeyName2 = Value2; 

    segment name and the key names are in use too case insensitive, but the name can not have spaces. 

    Key may be stored Integer, String data type, float, boolean, datatime like. 

Examples As I wrote the service configuration file database connection: 

[Oralce] 

con_scyx = Provider = MSDAORA. 1 ; Password = ygscyx; the User ID = scyx; the Data Source = ygdbsrv; of the Persist Security Info = True 

con_erp = Provider = MSDAORA. 1 ; the ERP = Password; the ERP the User ID =; the Data the Source = ORCL; the Persist Security Info = True

 

   Second, reads the configuration file 

with a class in Delphi TIniFile for accessing a .ini file, the class file is defined in IniFiles.pas, particularly used as 

a definition of 
an increase in IniFiles Uses of Interface section; 

2 , in Var variable definition section increasing line: myinifile: Tinifile; 

can then be myinifile variables to create, open, read, write and other operations. 

Second, open the INI file 

    Filename: = ExtractFilePath (ParamStr ( 0 )) + ' program.ini ' ; 

    myinifile: . = Tinifile the Create (filename); 

     the statement will be a variable with a particular file program.ini myinifile establish contact, then you can, through variable myinifile, to read and write the value of the keywords in the file program.ini. If the parentheses does not specify the file name path, then this Program.ini files are stored in the Windows directory, the method Program.ini file is stored in the application's current directory is: name assigned full path and file.  

Third, the key value read

INI file support for the string, integer values, Boolean three types of data, TINIfiles provides three different classes of object methods to read the value INI file keyword. Suppose defined variables vs, vi, vb respectively, string, integer, boolean type. Which returns the default value of this key does not exist INI file the default value. 

   VS: = myinifile.Readstring ( ' section name ' , ' key ' , the default value); 

   VI: = myinifile.Readinteger ( ' section name ' , ' key ' , the default value); 

VB: = myinifile.Readbool ( ' section name ' , ' key ' , the default value); 

four, INI file write 

the same, TInifile class also provides three different object method, an INI file is written to the string, integer, and Boolean types of keywords. 

myinifile.writestring ( ' section name '' Key ' , or string variable value); 

myinifile.writeinteger ( ' section name ' , ' key ' , or variable integer value); 

myinifile.writebool ( ' section name ' , ' key ' , or variable True or False); 

when the INI file does not exist, the above statements also automatically creates the INI file. 

V. deleting keywords 

in addition to a method of increasing the available writing keyword, Tinifile class also provides a method of object delete keywords: 

myinifile.DeleteKey ( ' section name ' , ' key ' ); 

Six, the operation section 

increases a measure the method can be used to complete the write 

myinifile.EraseSection ( ' section name ' );                     To delete a section 

myinifile.readsection ( ' section name ' , TStrings variable); reads all the keywords section name to a string variable list; myinifile.readsections (TStrings variable); all the sections were read INI file to take a list of strings variable. 

myinifile.readsectionvalues ( ' section name ' , of TStrings variable); the key sections of the read character string to a list of variable. 

Seven release 

myinifile. The Destroy ; may also be used FreeAndNil (myinifile); 

 

examples I wrote read the configuration file database 

Procedure TDBMonitorService.ServiceCreate (Sender: TObject);
 var 
, pFile: Array [ 0 .. 255 ] of char;
 the begin 
the try 
the GetModuleFileName ( 0, pFile, Sizeof (pFile)); // get the path 
g_Path: = ExtractFileDir (pFile); // path to get access to services directory name from the file name (not the file is not in the root directory value obtained when "/" as in the root directory, the letter is, for example, "C: /")    
with . tinifile the Create (g_path + ' \ the config.ini ' ) do 
the try 
ConScyx: = the ReadString ( ' Oralce ' , ' con_scyx ' , '' ); 
ConErp: = the ReadString ( ' Oralce ' , ' con_erp ' , '' ); 
 the finally 
as Free; 
End ;


con_scyx:=TADOConnection.Create(nil);
con_scyx.ConnectionString:=ConScyx;
con_scyx.LoginPrompt:=false;

con_erp:=TADOConnection.Create(nil);
con_erp.ConnectionString:=ConErp;
con_erp.LoginPrompt:=false;

qry_scyx:=TADOQuery.Create(nil);
qry_scyx.Connection:=con_scyx;
qry_erp:=TADOQuery.Create(nil);
qry_erp.Connection:=con_erp; 

the except 
ON E: Exception do 
the begin 
WriteLog (e.Message + ' -> service startup Error! ' ); 
the Exit; 
End ;
 End ;
 End ;

 

Guess you like

Origin www.cnblogs.com/jijm123/p/11299147.html