Delphi 对ini文件的操作
界面如图:
代码如下:
1 unit Unit1; 2 3 interface 4 5 uses 6 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 7 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,IniFiles; //添加库 IniFiles 8 9 type 10 TForm1 = class(TForm) 11 Button1: TButton; 12 Button2: TButton; 13 Label1: TLabel; 14 Label2: TLabel; 15 Label3: TLabel; 16 Label4: TLabel; 17 Edit1: TEdit; 18 Edit2: TEdit; 19 Edit3: TEdit; 20 CheckBox1: TCheckBox; 21 procedure Button1Click(Sender: TObject); 22 procedure Button2Click(Sender: TObject); 23 private 24 { Private declarations } 25 public 26 { Public declarations } 27 end; 28 29 var 30 Form1: TForm1; 31 var 32 filepath :string; 33 myinifile : TIniFile; 34 implementation 35 36 {$R *.dfm} 37 38 procedure TForm1.Button1Click(Sender: TObject); 39 begin 40 try 41 filepath := ExtractFilePath(Paramstr(0)) + 'serverlist.ini'; //获取当前路径+文件名 42 myinifile := Tinifile.Create(filepath); //创建文件 43 except 44 ShowMessage('LOADINI打开配置文件失败'); 45 Exit; 46 end; 47 {写入} 48 myinifile.WriteString('ListServer1','ServerCount','1'); 49 myinifile.WriteString('ListServer1','Name0','1'); 50 myinifile.WriteString('ListServer1001','ServerCount','1'); 51 myinifile.WriteString('ListServer1001','Name0',Edit1.Text); 52 myinifile.WriteString('ListServer1001','IP0',Edit2.Text); 53 myinifile.WriteInteger('ListServer1001','Port0',StrToInt(Edit3.Text)); 54 myinifile.WriteBool('Battle','Enable',False); 55 56 myinifile.WriteInteger('Resolution','width',1024); 57 myinifile.WriteInteger('Resolution','height',768); 58 if CheckBox1.Checked = True then 59 myinifile.WriteBool('WINDOW_MODE','MODE',True) 60 else 61 myinifile.WriteBool('WINDOW_MODE','MODE',False); 62 ShowMessage('写入成功'); 63 end; 64 65 procedure TForm1.Button2Click(Sender: TObject); 66 begin 67 try 68 filepath := ExtractFilePath(Paramstr(0)) + 'serverlist.ini'; //获取当前路径+文件名 69 myinifile := Tinifile.Create(filepath); //创建文件 70 except 71 ShowMessage('LOADINI打开配置文件失败'); 72 Exit; 73 end; 74 {读取} 75 Edit1.Text:= myinifile.ReadString('ListServer1001','Name0',''); 76 Edit2.Text:= myinifile.ReadString('ListServer1001','IP0',''); 77 Edit3.Text:= myinifile.ReadString('ListServer1001','Port0',''); 78 end; 79 80 end.