自己为了使用方便写的

//***************************************************************
//说明:设置信息保存类,支持Reg、XML、Ini
//作者:Garfield
//创建时间:2004-11-18
//最后修改时间:2004-11-18
//修改历史:
//1.调试通过Reg,Ini,XML有问题
//XMLCreate(nil)或Create(FileName)都有问题,只有Create(Application)时才可以
//并且设置值时有问题,对XML不熟悉
//
//****************************************************************
unit Config;

interface

uses
  SysUtils, Windows, Messages, Classes, Graphics, Controls,
  Forms, Dialogs,XMLDoc,registry,IniFiles;

type
  TConfig = class(TObject)
  private
    FConfigType: Integer;
    FFileName: string;
    FSession: string;
  public
    constructor Create(Session,FileName:String); virtual;
    destructor Destroy; virtual; abstract;
    function ReadSetting(Key,Default:ShortString): string; virtual; abstract;
    procedure WriteSetting(Key,Value:ShortString); virtual; abstract;
  published
    property ConfigType: Integer read FConfigType write FConfigType;
  end;
  TXMLConfig = class(TConfig)
  private
    FXMLDoc: TXMLDocument;
  public
    constructor Create(Session,FileName:String); override;
    destructor Destroy; override;
    function ReadSetting(Key,Default:ShortString): string; override;
    procedure WriteSetting(Key,Value:ShortString); override;
  end;
  TRegConfig = class(TConfig)
  private
    FReg: TRegistry;
  public
    constructor Create(Session,FileName:String); override;
    destructor Destroy; override;
    function ReadSetting(Key,Default:ShortString): string; override;
    procedure WriteSetting(Key,Value:ShortString); override;
  end;
  TIniConfig = class(TConfig)
  private
    FIniFile: TIniFile;
  public
    constructor Create(Session,FileName:String); override;
    destructor Destroy; override;
    function ReadSetting(Key,Default:ShortString): string; override;
    procedure WriteSetting(Key,Value:ShortString); override;
  end;

implementation

{
*********************************** TConfig ************************************
}
constructor TConfig.Create(Session,FileName:String);
begin
  FSession:=Session;
  FFileName:=FileName;
end;

{
********************************** TXMLConfig **********************************
}
constructor TXMLConfig.Create(Session,FileName:String);
begin
  inherited;
  //如果不存在XML文件
  if not FileExists(FFileName) then
  begin
    //+++++++++++++++++++++++++++++++++++++
    with (TXMLDocument.Create(Application)) do
    begin
      XML.Add('<?xml version="1.0" standalone="yes"?>');
      XML.Add('<'+FSession+'>');
      XML.Add('Carfield');
      XML.Add('</'+FSession+'>');
      Active := true;
      SaveToFile(FFileName);
      Free;
    end;
  //+++++++++++++++++++++++++++++++++++++
  end;
  FXMLDoc:=TXMLDocument.create(Application);
  FXMLDoc.FileName:=FFileName;
  FXMLDoc.Active:=True;
end;

destructor TXMLConfig.Destroy;
begin
  FXMLDoc.Active:=False;
  FXMLDoc.Free;
end;

function TXMLConfig.ReadSetting(Key,Default:ShortString): string;
begin
  try
     //result:=FXMLDoc.DocumentElement.ChildNodes[FSession].ChildNodes[Key].Text;
     result:=FXMLDoc.DocumentElement.ChildNodes[Key].Text;
  except
     Result:=Default;
  end;
end;

procedure TXMLConfig.WriteSetting(Key,Value:ShortString);
begin
  //FXMLDoc.DocumentElement.ChildNodes[FSession].ChildNodes[Key].Text:=Value;
  FXMLDoc.DocumentElement.ChildNodes[Key].Text:=Value
end;

{
********************************** TRegConfig **********************************
}
constructor TRegConfig.Create(Session,FileName:String);
begin
  inherited;
  FReg:=TRegistry.Create ;
end;

destructor TRegConfig.Destroy;
begin
  FReg.Free;
end;

function TRegConfig.ReadSetting(Key,Default:ShortString): string;
var
  v: string;
begin
  try
  //    FReg.RootKey :=HKEY_CURRENT_USER;
    if FReg.OpenKey(FSession,False) then
    begin
      FReg.OpenKey(FSession,False);
      v:=FReg.ReadString(Key);
      if v='' then  Result:=Default else Result:=v;
    end
  finally
      FReg.CloseKey ;
  end;
end;

procedure TRegConfig.WriteSetting(Key,Value:ShortString);
begin
  try
  //    Reg.RootKey :=HKEY_CURRENT_USER;
     if FReg.OpenKey(FSession,True) then
     begin
       FReg.OpenKey(FSession,False);
       FReg.WriteString(Key,Value);
     end
   finally
       FReg.CloseKey ;
   end;
end;

{
********************************** TIniConfig **********************************
}
constructor TIniConfig.Create(Session,FileName:String);
begin
  inherited;
  FIniFile:=TIniFile.create(FFileName);
end;

destructor TIniConfig.Destroy;
begin
  FIniFile.Free;
end;

function TIniConfig.ReadSetting(Key,Default:ShortString): string;
begin
  try
     result:=FiniFile.ReadString(FSession,Key,Default);
  except
     Result:=Default;
  end;
end;

procedure TIniConfig.WriteSetting(Key,Value:ShortString);
begin
  try
     FiniFile.WriteString(FSession,Key,Value);
  except
  end;
end;

end.

posted on 2009-04-01 13:01  garfieldtom  阅读(890)  评论(0编辑  收藏  举报