delphi读写ini文件实例 //--两个过程,主要实现:窗体关闭的时候,文件保存界面信息;窗体创建的时候,程序读取文件文件保存的信息。 //--首先要uses IniFiles(单元) //--窗体创建的时候,读取ini文件信息 procedure TfrmAFN04H_F9.FormCreate(Sender: TObject); var vFIni: TIniFile; sFileName: string; begin sFileName := ExtractFileDir(Application.ExeName); if Copy(sFileName, Length(sFileName) - 1, 1) = ‘/‘ then sFileName := sFileName + ‘saveInformation.ini‘ else sFileName := sFileName + ‘/‘ + ‘saveInformation.ini‘; vFIni := TIniFIle.Create(sFileName); Edit1.Text := vFIni.ReadString(‘AFN04F7‘, ‘Edit1‘, ‘0‘); Edit2.Text := vFIni.ReadString(‘AFN04F7‘, ‘Edit2‘, ‘0‘); Edit3.Text := vFIni.ReadString(‘AFN04F7‘, ‘Edit1‘, ‘0‘); Edit4.Text := vFIni.ReadString(‘AFN04F7‘, ‘Edit4‘, ‘0‘); end; //--窗体关闭的时候,文件保存界面信息; procedure TfrmAFN04H_F9.FormClose(Sender: TObject; var Action: TCloseAction); var sFileName: string; vFIni: TIniFile; iTmp, i: integer; name: string; begin //写配置文件 sFileName := ExtractFileDir(Application.ExeName); if Copy(sFileName, Length(sFileName) - 1, 1) = ‘/‘ then sFileName := sFileName + ‘saveInformation.ini‘ else sFileName := sFileName + ‘/‘ + ‘saveInformation.ini‘; vFIni := TIniFIle.Create(sFileName); try vFIni.WriteString(‘AFN04F9‘, ‘Edit1‘, Edit1.Text); vFIni.WriteString(‘AFN04F9‘, ‘Edit2‘, Edit2.Text); vFIni.WriteString(‘AFN04F9‘, ‘Edit3‘, Edit3.Text); vFIni.WriteString(‘AFN04F9‘, ‘Edit4‘, Edit4.Text); finally vFIni.Free; end; end; [delphi] view plaincopy uses IniFiles; //简单数据类型 TSimpleType = (stInt, stFloat, stString, stDateTime, stDate, stTime, stBoolean); function ReadIniValue(const FileName, Section, Name: string; SimpleType: TSimpleType; DefaultValue: Variant): Variant; // 读Ini文件的函数 // FileName:Ini文件名 // Section:节点 // Name:字段名 // SimpleType:简单数据类型 // DefaultValue:默认值 // 返回变体类型 begin with TIniFile.Create(FileName) do try if SimpleType = stString then Result := ReadString(Section, Name, DefaultValue) else if SimpleType = stInt then Result := ReadInteger(Section, Name, DefaultValue) else if SimpleType = stFloat then Result := ReadFloat(Section, Name, DefaultValue) else if SimpleType = stDateTime then Result := ReadDateTime(Section, Name, DefaultValue) else if SimpleType = stDate then Result := ReadDate(Section, Name, DefaultValue) else if SimpleType = stTime then Result := ReadTime(Section, Name, DefaultValue) else if SimpleType = stBoolean then Result := ReadBool(Section, Name, DefaultValue); finally Free; end; procedure WriteIniValue(const FileName, Section, Name: string; Value: Variant; SimpleType: TSimpleType); // 写INI文件的函数 // FileName:Ini文件名 // Section:节点 // Name:字段名 // Value:字段值 // SimpleType:简单数据类型 begin with TIniFile.Create(FileName) do try if SimpleType = stString then WriteString(Section, Name, VarToStr(Value)) else if SimpleType = stInt then WriteInteger(Section, Name, Value) else if SimpleType = stFloat then WriteFloat(Section, Name, Value) else if SimpleType = stDateTime then WriteDateTime(Section, Name, VarToDateTime(Value)) else if SimpleType = stDate then WriteDate(Section, Name, VarToDateTime(Value)) else if SimpleType = stTime then WriteTime(Section, Name, VarToDateTime(Value)) else if SimpleType = stBoolean then WriteBool(Section, Name, Value); finally Free; end; end; 调用范例: WriteIniValue(‘c:/config.ini‘,‘constring‘,‘db‘,‘aaa‘,stString); WriteIniValue(‘c:/config.ini‘,‘constring‘,‘port‘,1,stInt); ReadIniValue(‘c:/config.ini‘,‘constring‘,‘dbname‘,stString,‘misdate‘); ReadIniValue(‘c:/config.ini‘,‘constring‘,‘connect‘,stInt,-1); WriteIniValue(‘c:/中国烂鞋.ini‘,‘锅家队‘,‘猪教练‘,‘郭十二‘,stString); WriteIniValue(‘c:/中国烂鞋.ini‘,‘锅家队‘,‘平均罚球‘,1,stInt); ReadIniValue(‘c:/中国烂鞋.ini‘,‘锅家队‘,‘猪教练‘,stString,‘郭12‘); ReadIniValue(‘c:/中国烂鞋.ini‘,‘锅家队‘,‘平均罚球‘,stInt,-1); [delphi] view plaincopy 1.Delphi读写ini文件需要引用‘IniFiles‘单元 2.定义变量: Var Filename:string; myinifile:Tinifile; 3.变量附值 Filename:=ExtractFilePath(Paramstr(0))+‘program.ini‘;//指明路径.如果不指明路径.文件将在windows目录建立 myinifile:=Tinifile.Create(Filename);//Create(‘program.ini‘); 4.写入文件 myinifile.writestring(小节名,关键字,变量或者字符串值); myinifile.writeinteger(小节名,关键字,变量或者整数数值); myinifile.writenbool(小节名,关键字,变量或者True/False); 如果ini文件不存在的时候.会自动创建该ini文件 5.删除关键字 myinifile.DeleteKey(小节名,关键字); 6.删除小节 myinifile.EraseSection(小节名); 7.释放变量 myinifile.distory; 8.其他 Tinifile类还提供了3种对象方法来操作ini文件 ①myinifile.readsection(小节名,TStrings变量);可将指定小节中的所有关键字名读取至一个字符串列表变量中; ②myinifile.readsections(TStrings变量);可将INI文件中所有小节名读取至一个字符串列表变量中去. ③myinifile.readsectionvalues(小节名,TStrings变量);可将INI文件中指定小节的所有行(包括关键字、=、值)读取至一个字符串列表变量中去. 9. delphi读写ini文件实例 //--两个过程,主要实现:窗体关闭的时候,文件保存界面信息;窗体创建的时候,程序读取文件文件保存的信息。 //--首先要uses IniFiles(单元) //--窗体创建的时候,读取ini文件信息 procedure TfrmAFN04H_F9.FormCreate(Sender: TObject); var vFIni: TIniFile; sFileName: string; begin sFileName := ExtractFileDir(Application.ExeName); if Copy(sFileName, Length(sFileName) - 1, 1) = ‘/‘ then sFileName := sFileName + ‘saveInformation.ini‘ else sFileName := sFileName + ‘/‘ + ‘saveInformation.ini‘; vFIni := TIniFIle.Create(sFileName); Edit1.Text := vFIni.ReadString(‘AFN04F7‘, ‘Edit1‘, ‘0‘); Edit2.Text := vFIni.ReadString(‘AFN04F7‘, ‘Edit2‘, ‘0‘); Edit3.Text := vFIni.ReadString(‘AFN04F7‘, ‘Edit1‘, ‘0‘); Edit4.Text := vFIni.ReadString(‘AFN04F7‘, ‘Edit4‘, ‘0‘); end; //--窗体关闭的时候,文件保存界面信息; procedure TfrmAFN04H_F9.FormClose(Sender: TObject; var Action: TCloseAction); var sFileName: string; vFIni: TIniFile; iTmp, i: integer; name: string; begin //写配置文件 sFileName := ExtractFileDir(Application.ExeName); if Copy(sFileName, Length(sFileName) - 1, 1) = ‘/‘ then sFileName := sFileName + ‘saveInformation.ini‘ else sFileName := sFileName + ‘/‘ + ‘saveInformation.ini‘; vFIni := TIniFIle.Create(sFileName); try vFIni.WriteString(‘AFN04F9‘, ‘Edit1‘, Edit1.Text); vFIni.WriteString(‘AFN04F9‘, ‘Edit2‘, Edit2.Text); vFIni.WriteString(‘AFN04F9‘, ‘Edit3‘, Edit3.Text); vFIni.WriteString(‘AFN04F9‘, ‘Edit4‘, Edit4.Text); finally vFIni.Free; end; end;
原文地址:https://www.cnblogs.com/jijm123/p/11296251.html
时间: 2024-11-03 08:51:05