Delphi读写ini配置文件控件封装

{
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;

interface

uses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
    IniFiles, Variants;
type
  TIniType = class(TObject)
    FValue : String;
    private
      function ValueToInt : Integer;
      function ValueToBoolean : Boolean;
    public
      property Value : String read FValue;
      property ToInt : Integer read ValueToInt;
      property ToBoolean : Boolean read ValueToBoolean;
  end;

type
  TIniValue = record
    name : String;
    AsString : String;
    AsInt : Integer;
    AsBoolean : Boolean;
  end;
  TIniValues = record
    name : String;
    count : Integer;
    iniValue : array of TIniValue;
  end;

  TSections = record
    count : Integer;
    iniValues : array of TIniValues;
  end;  

type
  TChangeEvent = procedure(Sender: TObject) of object;

  TWiIniCom = class(TComponent)
  private

  protected
	{ Protected declarations }
    FIniFile : String;
    FSection : String;
    FActive  : Boolean; 
    procedure SetIniFile (strFileName: string);
    procedure SetSection (strName: string);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    Fini : 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;
begin
    RegisterComponents('WiCom', [TWiIniCom])
end;

// 控件创建
constructor TWiIniCom.Create(AOwner: TComponent);
begin
    inherited Create(AOwner);
end;

// 控件销毁
destructor TWiIniCom.Destroy;
begin
    inherited Destroy;
    FIni.Free;
end;

// 设置ini文件
procedure TWiIniCom.SetIniFile(strFileName: string);
var
    strlistFile : TStringList;
begin
    if not FileExists(strFileName) then
    begin
        strlistFile := TStringList.Create;
        strlistFile.Text := '';
        strlistFile.SaveToFile(strFileName);
        strlistFile.Free;
    end;
    FIniFile := strFileName;
    FIni := TIniFile.Create(strFileName);
end;

// 设置section
procedure TWiIniCom.SetSection(strName: String);
begin
    FSection := strName;
end;

// 取文件信息
function TWiIniCom.GetList : TSections;
var
    ini : TIniFile;
    slst, ilst : TStringList;
    i, j, scount, icount : Integer;
    sname, istr, str : String;
begin
    slst := 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 do
    begin
        sname := 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 do
        begin
            istr := 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;
var
    ini : TIniFile;
begin
    if not FileExists(FIniFile) then
        Exit ;
    try
        ini := TIniFile.Create(FIniFile) ;
        result := ini.ReadString(Section, Ident, '') ;
    finally
        ini.Free ;
    end;
end;

// 写ini文件配置项信息
function TWiIniCom.WriteFile(const Section, Ident, value: string) : Boolean;
var
  ini : TIniFile;
begin
    result := false;
    if not FileExists(FIniFile) then
        Exit;
    try
        ini := TIniFile.Create(FIniFile);
        ini.WriteString(Section, Ident, value);
        result := true;
    finally
        ini.Free;
    end;
end;

// 读取ini文件Section信息
function TWiIniCom.ReadSection(const Ident : String) : String;
begin
    result := ReadFile(FSection, Ident);
end;

// 写ini文件Section信息
function TWiIniCom.WriteSection(const Ident, value : String) : Boolean;
begin
    result := WriteFile(FSection, Ident, value);
end;

// 值转int
function TIniType.ValueToInt : Integer;
begin
    result := StrToInt(FValue);
end;

// 值转bool
function TIniType.ValueToBoolean : Boolean;
var
    strValue : String;
begin
    strValue := LowerCase(FValue);
    if ( strValue = '1' ) or (strValue = 'true') then
        result := true
    else
        result := false;
end;

// 读取Ident项信息
function TWiIniCom.ReadIdent(const Ident: string) : TIniValue;
var
    str : String;
begin
    result.AsString := '';
    result.AsInt := $FFFFFFFF;
    result.AsBoolean := false;
    str := ReadFile(FSection, Ident);
    result.AsString := str;

    try
        result.AsInt := StrToIntDef(str, $FFFFFFFF);
    except
    end;

    str := LowerCase(Trim(str));
    if (str = '1') or (str = 'true') then
        result.AsBoolean := true;

end;

// 写Ident项信息
function TWiIniCom.WriteIdent (const Ident : String; const value : Variant) : Boolean;
var
    strValue : String;
begin
    strValue := VarToStr(value);
    result := WriteFile(FSection, Ident, strValue);
end;

end.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值