{
Wilmer ini文件处理控件例子:
c:\config.ini文件内容:
----------------------------------------------------------------------
[main]
Config1=123456abc
----------------------------------------------------------------------调用方法:
----------------------------------------------------------------------
// 加载ini文件
WiIniCom.IniFile := 'c:\config.ini';
// 读取Config1项信息
strValue := WiIniCom.ReadFile('main', 'Config1');
或者:
WiIniCom.Section := 'main';
strValue := WiIniCom.ReadIdent('Config1').AsString;// 写Config1项信息
WiIniCom.WriteFile('main', 'Config1', strValue);
----------------------------------------------------------------------
}unit WiIniCom;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,IniFiles, Variants;
typeTIniType = class(TObject)FValue : String;privatefunction ValueToInt : Integer;function ValueToBoolean : Boolean;publicproperty Value : String read FValue;property ToInt : Integer read ValueToInt;property ToBoolean : Boolean read ValueToBoolean;end;typeTIniValue = recordname : String;AsString : String;AsInt : Integer;AsBoolean : Boolean;end;TIniValues = recordname : String;count : Integer;iniValue : array of TIniValue;end;TSections = recordcount : Integer;iniValues : array of TIniValues;end; typeTChangeEvent = procedure(Sender: TObject) of object;TWiIniCom = class(TComponent)privateprotected{ Protected declarations }FIniFile : String;FSection : String;FActive : Boolean; procedure SetIniFile (strFileName: string);procedure SetSection (strName: string);publicconstructor Create(AOwner: TComponent); override;destructor Destroy; override;publishedFini : TIniFile;property IniFile : String read FIniFile write SetIniFile;property Section : String read FSection write SetSection;function ReadFile(const Section, Ident : String) : String;function WriteFile(const Section, Ident, value : String) : Boolean;function ReadSection(const Ident : String) : String;function ReadIdent(const Ident : String) : TIniValue;function WriteIdent (const Ident : String; const value : Variant) : Boolean;function WriteSection(const Ident, value : String) : Boolean;function GetList : TSections;property Active : Boolean read FActive write FActive default true;end;procedure Register;implementation// 注册控件WiCom页
procedure Register;
beginRegisterComponents('WiCom', [TWiIniCom])
end;// 控件创建
constructor TWiIniCom.Create(AOwner: TComponent);
begininherited Create(AOwner);
end;// 控件销毁
destructor TWiIniCom.Destroy;
begininherited Destroy;FIni.Free;
end;// 设置ini文件
procedure TWiIniCom.SetIniFile(strFileName: string);
varstrlistFile : TStringList;
beginif not FileExists(strFileName) thenbeginstrlistFile := TStringList.Create;strlistFile.Text := '';strlistFile.SaveToFile(strFileName);strlistFile.Free;end;FIniFile := strFileName;FIni := TIniFile.Create(strFileName);
end;// 设置section
procedure TWiIniCom.SetSection(strName: String);
beginFSection := strName;
end;// 取文件信息
function TWiIniCom.GetList : TSections;
varini : TIniFile;slst, ilst : TStringList;i, j, scount, icount : Integer;sname, istr, str : String;
beginslst := TStringList.Create;ini := TIniFile.Create(FIniFile);ini.ReadSections(slst);scount := slst.Count;SetLength(result.iniValues, scount);result.count := scount;for i := 0 to scount - 1 dobeginsname := slst.Strings[i];result.iniValues[i].name := sname;ilst := TStringList.Create;ini.ReadSection(sname, ilst);icount := ilst.Count;SetLength(result.iniValues[i].iniValue, icount);result.iniValues[i].count := icount; for j := 0 to icount - 1 dobeginistr := ilst.Strings[j];result.iniValues[i].iniValue[j].name := istr;str := ini.ReadString(sname, istr, ''); result.iniValues[i].iniValue[j].AsString := str;result.iniValues[i].iniValue[j].AsInt := StrToIntDef(str, $FFFFFFFF);end;ilst.Free;end;slst.Free;ini.Free;
end;// 读取ini文件配置项信息
function TWiIniCom.ReadFile(const Section, Ident: string) : string;
varini : TIniFile;
beginif not FileExists(FIniFile) thenExit ;tryini := TIniFile.Create(FIniFile) ;result := ini.ReadString(Section, Ident, '') ;finallyini.Free ;end;
end;// 写ini文件配置项信息
function TWiIniCom.WriteFile(const Section, Ident, value: string) : Boolean;
varini : TIniFile;
beginresult := false;if not FileExists(FIniFile) thenExit;tryini := TIniFile.Create(FIniFile);ini.WriteString(Section, Ident, value);result := true;finallyini.Free;end;
end;// 读取ini文件Section信息
function TWiIniCom.ReadSection(const Ident : String) : String;
beginresult := ReadFile(FSection, Ident);
end;// 写ini文件Section信息
function TWiIniCom.WriteSection(const Ident, value : String) : Boolean;
beginresult := WriteFile(FSection, Ident, value);
end;// 值转int
function TIniType.ValueToInt : Integer;
beginresult := StrToInt(FValue);
end;// 值转bool
function TIniType.ValueToBoolean : Boolean;
varstrValue : String;
beginstrValue := LowerCase(FValue);if ( strValue = '1' ) or (strValue = 'true') thenresult := trueelseresult := false;
end;// 读取Ident项信息
function TWiIniCom.ReadIdent(const Ident: string) : TIniValue;
varstr : String;
beginresult.AsString := '';result.AsInt := $FFFFFFFF;result.AsBoolean := false;str := ReadFile(FSection, Ident);result.AsString := str;tryresult.AsInt := StrToIntDef(str, $FFFFFFFF);exceptend;str := LowerCase(Trim(str));if (str = '1') or (str = 'true') thenresult.AsBoolean := true;end;// 写Ident项信息
function TWiIniCom.WriteIdent (const Ident : String; const value : Variant) : Boolean;
varstrValue : String;
beginstrValue := VarToStr(value);result := WriteFile(FSection, Ident, strValue);
end;end.