项目以Rtti特性做文件参数配置,简化每项读写ini操作,记录以做备忘,代码如下:

unit uGlobal;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Forms, TypInfo, IniFiles; type
TConfigBase = class(TPersistent)
private
FFileName: string;
procedure Load(AIniFile: TIniFile); overload;
procedure Save(AIniFile: TIniFile); overload;
protected
function GetSectionName: string; virtual;
procedure LoadProperty(PropInfo: PPropInfo; const Section: string; IniFile: TIniFile); virtual;
procedure SaveProperty(PropInfo: PPropInfo; const Section: string; IniFile: TIniFile); virtual;
public
procedure Load(AFileName: string); overload;
procedure Save(AFileName: string); overload;
end; TLiveUpdate = class; TApp = class(TConfigBase)
private
FAppName: string;
FVersion: string;
FShowMsg: Boolean;
FProductID: Integer;
FWindowState: TWindowState;
FLiveUpdate: TLiveUpdate;
protected
function GetSectionName: string; override;
public
constructor Create;
destructor Destroy; override;
published
property AppName: string read FAppName write FAppName;
property Version: string read FVersion write FVersion;
property ShowMsg: Boolean read FShowMsg write FShowMsg;
property ProductID: Integer read FProductID write FProductID;
property WindowState: TWindowState read FWindowState write FWindowState;
property LiveUpdate: TLiveUpdate read FLiveUpdate write FLiveUpdate;
end; TLiveUpdate = class(TConfigBase)
private
FAvailabled: Boolean;
FAutoUpdate: Boolean;
FUpdatePeriod: Integer;
FLastUpdateDate: TDateTime;
protected
procedure LoadProperty(PropInfo: PPropInfo; const Section: string; IniFile: TIniFile); override;
procedure SaveProperty(PropInfo: PPropInfo; const Section: string; IniFile: TIniFile); override;
public
constructor Create;
published
property Availabled: Boolean read FAvailabled write FAvailabled;
property AutoUpdate: Boolean read FAutoUpdate write FAutoUpdate;
property UpdatePeriod: Integer read FUpdatePeriod write FUpdatePeriod;
property LastUpdateDate: TDateTime read FLastUpdateDate write FLastUpdateDate;
end; function App: TApp; implementation var
FApp: TApp; function App: TApp;
begin
if FApp = nil then
FApp := TApp.Create;
Result := FApp;
end; { TConfigBase } function TConfigBase.GetSectionName: string;
begin
Result := ClassName;
if Result[] = 'T' then
Result := Copy(Result, , Length(Result) - );
end; procedure TConfigBase.Load(AFileName: string);
var
IniFile: TIniFile;
begin
if not FileExists(AFileName) then Exit; IniFile := TIniFile.Create(AFileName);
try
Load(IniFile);
finally
IniFile.Free;
end;
end; procedure TConfigBase.Load(AIniFile: TIniFile);
var
I, Count: Integer;
PropInfo: PPropInfo;
PropList: PPropList;
Section: string;
begin
Count := GetTypeData(ClassInfo)^.PropCount;
if Count = then Exit; GetMem(PropList, Count * SizeOf(Pointer));
try
Section := GetSectionName;
GetPropInfos(ClassInfo, PropList);
for I := to Count - do
begin
PropInfo := PropList^[I];
if (PropInfo <> nil) and IsStoredProp(self, PropInfo) and (PropInfo^.SetProc <> nil) then
LoadProperty(PropInfo, Section, AIniFile);
end;
finally
FreeMem(PropList);
end;
end; procedure TConfigBase.LoadProperty(PropInfo: PPropInfo; const Section: string;
IniFile: TIniFile);
var
PropType: PTypeInfo;
Obj: TObject;
begin
PropType := PropInfo^.PropType^;
if (PropType^.Kind <> tkClass) and (not IniFile.ValueExists(Section, string(PropInfo^.Name))) then Exit; case PropType^.Kind of
tkClass:
begin
Obj := GetObjectProp(self, PropInfo);
if Assigned(Obj) and (Obj is TConfigBase) then
TConfigBase(Obj).Load(IniFile);
end;
tkInteger, tkChar, tkWChar:
SetOrdProp(Self, PropInfo, IniFile.ReadInteger(Section, string(PropInfo.Name), PropInfo^.Default));
tkString, tkLString, tkUstring, tkWString:
SetStrProp(Self, PropInfo, IniFile.ReadString(Section, string(PropInfo.Name), ''));
tkEnumeration:
SetEnumProp(Self, PropInfo, IniFile.ReadString(Section, string(PropInfo.Name), ''));
tkSet:
SetSetProp(Self, PropInfo, IniFile.ReadString(Section, string(PropInfo.Name), ''));
tkFloat:
SetFloatProp(Self, PropInfo, IniFile.ReadFloat(Section, string(PropInfo^.Name), ));
end;
end; procedure TConfigBase.Save(AFileName: string);
var
IniFile: TIniFile;
begin
if AFileName = '' then Exit; IniFile := TIniFile.Create(AFileName);
try
try
Save(IniFile);
except on E: Exception do
OutputDebugString(PChar(Format('Exceptoin: save to file %s fail, err: %s', [AFileName, E.Message])));
end;
finally
IniFile.Free;
end;
end; procedure TConfigBase.Save(AIniFile: TIniFile);
var
I, Count: Integer;
PropInfo: PPropInfo;
PropList: PPropList;
Section: string;
begin
Section := GetSectionName;
Count := GetTypeData(ClassInfo)^.PropCount;
if Count > then
begin
GetMem(PropList, Count * SizeOf(Pointer));
try
GetPropInfos(ClassInfo, PropList);
for I := to Count - do
begin
PropInfo := PropList^[I];
if (PropInfo <> nil) and IsStoredProp(self, PropInfo) and (PropInfo^.GetProc <> nil) then
SaveProperty(PropInfo, Section, AIniFile);
end;
finally
FreeMem(PropList);
end;
end;
end; procedure TConfigBase.SaveProperty(PropInfo: PPropInfo; const Section: string;
IniFile: TIniFile);
var
PropType: PTypeInfo;
Obj: TObject;
const
Format_d = '%d';
begin
PropType := PropInfo^.PropType^;
case PropType^.Kind of
tkClass:
begin
Obj := GetObjectProp(self, PropInfo);
if Assigned(Obj) and (Obj is TConfigBase) then
TConfigBase(Obj).Save(IniFile);
end;
tkInteger, tkChar, tkWChar:
IniFile.WriteInteger(Section, string(PropInfo^.Name), GetOrdProp(Self, PropInfo));
tkString, tkLString, tkUstring, tkWString:
IniFile.WriteString(Section, string(PropInfo^.Name), GetWideStrProp(Self, PropInfo));
tkEnumeration:
IniFile.WriteString(Section, string(PropInfo^.Name), GetEnumName(PropType, GetOrdProp(Self, PropInfo)));
tkSet:
IniFile.WriteString(Section, string(PropInfo^.Name), GetSetProp(Self, PropInfo));
tkFloat:
IniFile.WriteFloat(Section, string(PropInfo^.Name), GetFloatProp(Self, PropInfo));
end;
end; { TApp } constructor TApp.Create;
begin
FFileName := ChangeFileExt(ParamStr(), '.ini');
FWindowState := wsNormal;
FLiveUpdate := TLiveUpdate.Create;
Load(FFileName);
end; destructor TApp.Destroy;
begin
Save(FFileName);
inherited Destroy;
end; function TApp.GetSectionName: string;
begin
Result := 'System';
end; { TLiveUpdate } constructor TLiveUpdate.Create;
begin
FAvailabled := True;
FAutoUpdate := True;
FUpdatePeriod := ;
FLastUpdateDate := Now;
end; procedure TLiveUpdate.LoadProperty(PropInfo: PPropInfo; const Section: string;
IniFile: TIniFile);
begin
if PropInfo^.Name = 'LastUpdateDate' then
FLastUpdateDate := IniFile.ReadDateTime(Section, string(PropInfo^.Name), Now)
else
inherited;
end; procedure TLiveUpdate.SaveProperty(PropInfo: PPropInfo; const Section: string;
IniFile: TIniFile);
begin
if PropInfo^.Name = 'LastUpdateDate' then
IniFile.WriteDateTime(Section, string(PropInfo^.Name), GetFloatProp(Self, PropInfo))
else
inherited;
end; end.

使用方法:

procedure TForm1.FormCreate(Sender: TObject);
begin
App.AppName := 'RttInfo';
App.Version := GetFileVersion(ParamStr());
end; procedure TForm1.FormDestroy(Sender: TObject);
begin
App.Free;
end;

比单个字段读写ini字段,省事

Delphi: RTTI与ini配置文件的更多相关文章

  1. (转载)将DELPHI数据库连接写进INI配置文件中

    将DELPHI数据库连接写进INI配置文件中 procedure TDM.DataModuleCreate(Sender: TObject); var piececonfg:Tinifile; pat ...

  2. C++读写ini配置文件GetPrivateProfileString()&WritePrivateProfileString()

    转载: 1.https://blog.csdn.net/fengbingchun/article/details/6075716 2. 转自:http://hi.baidu.com/andywangc ...

  3. DCMTK开源库的学习笔记4:利用ini配置文件对dcm影像进行归档

    转:http://blog.csdn.net/zssureqh/article/details/8846337 背景介绍: 医学影像PACS工作站的服务端需要对大量的dcm文件进行归档,写入数据库处理 ...

  4. 【个人使用.Net类库】(1)INI配置文件操作类

    开发接口程序时,对于接口程序配置的IP地址.端口等都需要是可配置的,而在Win Api原生实现了INI文件的读写操作,因此只需要调用Win Api中的方法即可操作INI配置文件,关键代码就是如何调用W ...

  5. 【Qt 】QSettings写ini配置文件

    QSettings写ini配置文件(Qt版本5.2): #include "inidemo.h" #include <QSettings> #include <Q ...

  6. c#读写ini配置文件示例

    虽然c#里都是添加app.config 并且访问也很方便 ,有时候还是不习惯用他.那么我们来做个仿C++下的那种ini配置文件读写吧     其他人写的都是调用非托管kernel32.dll.我也用过 ...

  7. vb ——ini 配置文件

    最近在学校VB 开发点小东西, 使用ini配置文件要用到下边连个函数 GetPrivateProfileString (从配置文件得到信息)百度百科的介绍http://baike.baidu.com/ ...

  8. python 提供INI配置文件的操作 ConfigParser

    原文地址:http://www.cnblogs.com/pumaboyd/archive/2008/08/11/1265416.html 红色的为标注信息 +++++++++++++++++引用+++ ...

  9. 第四十二节,configparser特定格式的ini配置文件模块

    configparser用于处理特定格式的文件,其本质上是利用open来操作文件. 特定格式的ini配置文件模块,用于处理ini配置文件,注意:这个ini配置文件,只是ini文件名称的文本文件,不是后 ...

随机推荐

  1. 正则表达式——WPF输入控件TextBox 限定输入特定字符

    概念: 正则表达式是对字符串操作的一种逻辑公式, 就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”, 这个“规则字符串”用来表达对字符串的一种过滤逻辑. 目的: 给定一个正 ...

  2. hive案例

    数据倾斜: 操作• Join on a.id=b.id• Group by• Count Distinct count(groupby)• 原因• key分布不均导致的• 人为的建表疏忽• 业务数据特 ...

  3. iOS 申请distribution证书, 公钥,私钥

    私钥只有在本机生成CSR文件的时候会产生,公钥会在CSR文件传给apple时,apple产生.

  4. Linux命令:索引

    目录 A B C D E F G H I  jobs J K L M N   nohup O P Q R S T  trU V W X Y Z A alias B C cd D dirs E F G ...

  5. Java IO流学习总结六:ByteArrayInputStream、ByteArrayOutputStream

    类的继承关系 InputStream |__ ByteArrayInputStream OutputStream |__ ByteArrayOutputStream ByteArrayInputStr ...

  6. Haskell语言学习笔记(70)NonEmpty

    NonEmpty(非空列表) infixr 5 :| data NonEmpty a = a :| [a] deriving (Eq, Ord) instance Functor NonEmpty w ...

  7. 转: JQuery this和$(this)的区别及获取$(this)子元素对象的方法

    1.JQuery this和$(this)的区别 相信很多刚接触JQuery的人,很多都会对$(this)和this的区别模糊不清,那么这两者有什么区别呢? 首先来看看JQuery中的  $()  这 ...

  8. [cocos2d-x]游戏开发基础(图)

    FreeMind的.mm文件下载: http://yunpan.cn/cfL3f5PXRfikP (提取码:90f1)

  9. 一秒去除Win7快捷方式箭头

    我相信有无数的小盆友跟我一样很讨厌Win7快捷方式图标上的箭头,实在太丑陋了,尤其是带有强迫症滴.现在介绍去除箭头的方式. 1. 打开编辑器,将以下代码粘贴进去,然后保存为.bat后缀的文件,然后双击 ...

  10. HttpURLConnection 添加代理

    //创建代理服务器 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("www.proxyaddress.com& ...