Delphi operation Ini file

 
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); Button5Click Procedure (Sender: TObject); Procedure Button6Click (Sender: TObject); Procedure Button7Click (Sender: TObject); Private {Private Declarations} public {Public Declarations} End; var the Form1: TForm1; Implementation {$ R & lt *}. Dfm uses IniFiles; {containing TIniFile unit uses} var ini: TIniFile; path: String; {ini file path section}, Key: String; {represent keywords ini file section} {ini file structure:; comments [section name. ] key = value INI file support: string, integer, boolean, Date , Time, DateTime, Double binary string type boolean value of true or false is represented by without quotes} 1,0 Procedure TForm1 .FormCreate (Sender: TObject); the begin path: = ChangeFileExt (ParamStr (= 'ADouble'; this333); Key := 'CBoolean'; ini.WriteBool(Section,Key,False); Key := 'CDate'; ini.WriteDate(Section,Key,Now); Key := 'CTime'; ini.WriteTime(Section,Key,Now); Key := 'CDateTime'; ini.WriteDateTime(Section,Key,Now); Key := 'CDouble'; ini.WriteFloat(Section,Key,Pi); {写入结果: [AAA] AString=AAA-String AInteger=111 ABoolean=1 ADate=2007-12-17 ATime=22:06:23 ADateTime=2007-12-17 22:06:23 ADouble=3.14159265358979 [BBB] BString=BBB-String BInteger=222 BBoolean=1 BDate=2007-12-17 BTime=22:06:23 BDateTime=2007-12-17 22:06:23 BDouble=3.14159265358979 [CCC] CString=CCC-String CInteger=333 CBoolean=0 CDate=2007-12-17 CTime=22:06:23 CDateTime=2007-12-17 22:06:23 CDouble=3.14159265358979 } end; // read the ini file: Procedure TForm1 .Button2Click (Sender: TObject); var S: String; I: Integer; B: Boolean; F: Double; D: tdate; T: TTIME; dt: a TDateTime; the begin S: = INI .ReadString ( 'the BBB', 'BString', ''); {last parameter is the default value I}: = INI .ReadInteger ( 'the BBB', 'BInteger', 0); B: = INI .ReadBool ( ' the BBB ', ' BBoolean ', False); F: = INI .ReadFloat ( ' the BBB ', ' BDouble ', 0); D: = INI .ReadDate ( ' the BBB ', ' BDate ', Now); T: = INI .ReadTime ( 'the BBB', 'btime', Now); dt: = INI .ReadDateTime ( 'BBB''BDateTime', Now); the ShowMessage (S); {String}-the ShowMessage the BBB (the IntToStr (I)); {222} the ShowMessage (BoolToStr (B)); {-1 (True)} the ShowMessage (FloatToStr (F)) ; {3.14159265358979} the ShowMessage (DateToStr you (D)); {} the ShowMessage 2007-12-17 (TimeToStr (T)); {} the ShowMessage 22:06:23 (DateTimeToStr (dt)); {2007-12-17 22 is: } 06:23 End; // name section to read all of TStrings: Procedure TForm1 .Button3Click (Sender: TObject); var List: of TStrings; the begin List: = a TStringList .Create; INI .ReadSections (List); the ShowMessage (List . the Text); {} the AAA List the BBB the CCC .free; End; // read the specified measures to all keywords of TStrings: Procedure TForm1.Button4Click(Sender: TObject); var List: TStrings; begin List := TStringList.Create; ini.ReadSection('AAA',List); ShowMessage(List.Text); { AString AInteger ABoolean ADate ATime ADateTime ADouble } List.Free; end;  //读入指定小节的所有关键字与值到 TStrings: procedure TForm1.Button5Click(Sender: TObject); var List: TStrings; begin List := TStringList.Create; ini.ReadSectionValues('BBB',List); ShowMessage(List.Text); { BString=BBB-String BInteger=222 BBoolean=1 BDate=2007-12-17 BTime=22:06:23 BDateTime=2007-12-17 22:06:23 BDouble=3.14159265358979 } List.Free; end; // delete and add Procedure TForm1 .Button6Click (Sender: TObject); the begin INI .DeleteKey ( 'the BBB', 'BString'); {INI deleting keywords} .EraseSection ( 'the CCC'); {delete section} // INI .UpdateFile; {saved to a file} {Add section with keywords or modified value, can be written directly} End; // other functions Procedure TForm1 .Button7Click (Sender: TObject); var B: Boolean; S: String; the begin B: = INI .SectionExists ( 'DDD'); {determines whether there is a section B}: = INI .ValueExists ( 'the AAA', 'AString'); {determines whether there is a keyword value} s: = INI .FileName; {get file name} End; Procedure TForm1.FormDestroy(Sender: TObject); begin ini.Free; end; end. 

 

 
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses IniFiles; var ini: TMemIniFile; procedure TForm1.FormCreate (Sender: TObject); the begin INI: = TMemIniFile .Create ( 'C: /temp/test.ini'); End; // write Procedure TForm1 .Button1Click (Sender: TObject); the begin INI .WriteString ( ' the AAA ', ' A1 ', ' String-the AAA '); // other and are also used as IniFile // ini.WriteInteger (); // ini.WriteBool (); // ini.WriteDate (); // INI .WriteTime (); // ini.WriteDateTime (); // ini.WriteFloat (); // ini.WriteBinaryStream (); //ini.UpdateFile; // because TMemIniFile a memory operation, so as to save the file to the end; // reading and other Procedure TForm1 .Button2Click (Sender: TObject); var S:String; the begin S: = INI .ReadString ( 'the AAA', 'A1', 'Default'); the ShowMessage (S); // String the AAA- // other and read commands are also used as IniFile // ini. ReadInteger (); // ini.ReadBool (); // ini.ReadDate (); // ini.ReadTime (); // ini.ReadDateTime (); // ini.ReadFloat (); // ini.ReadBinaryStream ( ); // there are four common methods are the same and IniFile //ini.DeleteKey (); //ini.EraseSection (); //ini.ReadSection (); //ini.ReadSections (); // also there are three methods IniFile are not easy to use //ini.GetStrings(List: of TStrings); //ini.SetStrings(List: of TStrings); //ini.Rename(const FileName: String; Reload: Boolean);// where Rename the second Boolean parameter to True if the refresh will read End; Procedure TForm1 .FormDestroy (Sender: TObject); the begin the INI .free; End; End.

Guess you like

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