delphi读写INI系统配置文件 一、调用delphi内建单元 uses System.IniFiles; 1、使用类TIniFile 2、类TIniFile的主要方法和函数: {$IFDEF MSWINDOWS} { TIniFile - Encapsulates the Windows INI file interface (Get/SetPrivateProfileXXX functions) } TIniFile = class(TCustomIniFile) public destructor Destroy; override; function ReadString(const Section, Ident, Default: string): string; override; procedure WriteString(const Section, Ident, Value: String); override; procedure ReadSection(const Section: string; Strings: TStrings); override; procedure ReadSections(Strings: TStrings); override; procedure ReadSectionValues(const Section: string; Strings: TStrings); override; procedure EraseSection(const Section: string); override; procedure DeleteKey(const Section, Ident: String); override; procedure UpdateFile; override; end; {$ELSE} TIniFile = class(TMemIniFile) public constructor Create(const FileName: string; const Encoding: TEncoding; CaseSensitive, UseLocale: Boolean); overload; override; end; {$ENDIF} 二、读写ini 2、INI文件的格式: [FDConnectionDefs.ini] Encoding=UTF8 [TCPServer] ServerName=192.168.3.100 Post=211 ChannelID=Channel1 ManagerID=Client_1 [MSSQL] Server=119.29.5.177 User_Name=sa Password=admin81 DATABASE=disp_tiyuxueyuan2015 DriverID=MSSQL Pooled=True MonitorBy=Remote Address=119.29.5.177,1433 3、初始化TIniFile文件类的一个实例: 编译分发应用时,先写入APP一个默认ini文件,然后对其进行读写操作: 3.1、MSWINDOWS var IniFile: TIniFile; FileName: string; aServerName: string; //FileName: 你要读写的ini文件名 //Create:用于初始化TIniFile文件类的一个实例: IniFile := TIniFile.Create(FileName); aServerName := IniFile.ReadString(‘TCPServer‘, ‘ServerName‘, ‘‘); //... IniFile.Free; 3.1.1、其中ReadString (读文件)、 WriteString (写文件) 、 DeleteKey (删除文件中的标志) 常用: 其参数含义: Section:代表INI文件中以[]分割的区块:比如上面的[TCPServer] Ident:参数的标志 Value:参数的数值(写) Default:参数的数值(读) 3.2、其它平台 var IniFileStream: TIniFile; Encoding: TEncoding; FileName: string; AStringList:TStringList; AName: string; //FileName: 你要读写的ini文件名 //Create:用于初始化TIniFile文件类的一个实例: Encoding:= TEncoding.Unicode; IniFileStream := TIniFile.Create(FileName,Encoding,false,true); AStringList:=TStringList.Create; AStringList.LoadFromFile(FileName); ///<summary>02、字符串被myDelimiter字符符号分隔符分割的数值获取,</summary> ///<summary>并将其赋值给一个 TStringList:</summary> AName:=‘‘; myStr:=AName; //写一个通用的//procedure myItems_Delimiter(myStr:String; myDelimiter:Char; AStringList:TStringList); myItems_Delimiter(AName,‘[‘,AStringList); //对AName.Trim进行操作(通过其中换行符就子串解析每一行,再用‘=‘号子串解析KEY及Value): //或AStringList循环遍历进行读写操作也可: //AStringList.Sorted: Boolean; //AStringList.Find(const S: string; var Index: Integer): Boolean //AStringList.IndexOf(const S: string): Integer; //AStringList.IndexOfName(const Name: string): Integer; //AStringList.Add(const S: string): Integer; //AStringList.Insert(Index: Integer; const S: string); //AStringList.Delete(Index: Integer); //AStringList.Exchange(Index1, Index2: Integer); AStringList.SaveToFile(FileName); //IniFileStream通过继承TMemIniFile继承TCustomIniFile而来 { TMemIniFile - loads an entire INI file into memory and allows all operations to be performed on the memory image. The image can then be written out to the disk file } {TMemIniFile -整个INI文件加载到内存中,并允许执行所有操作在内存映像(内存流)。 内存映像(内存流)可以进行读写等磁盘文件操作} //constructor Create(const FileName: string; const Encoding: TEncoding; //CaseSensitive, UseLocale: Boolean); overload; override; 3.2.1、其中以下构建解构及属性方法是上述TCustomIniFile继承以外的: constructor Create(CaseSensitive, UseLocale: Boolean); destructor Destroy; override; procedure Clear; function Remove(const Key: string): Boolean; property Count: Integer read GetCount; property CaseSensitive: Boolean read GetCaseSensitive write SetCaseSensitive; property UseLocale: Boolean read GetUseLocale write SetUseLocale; --------------------- 作者:pulledup 来源:CSDN 原文:https://blog.csdn.net/pulledup/article/details/93076568 版权声明:本文为博主原创文章,转载请附上博文链接!
原文地址:https://www.cnblogs.com/jijm123/p/11297264.html
时间: 2024-11-29 08:03:10