项目以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. day19-高阶函数、匿名函数

    map 函数 map 是一个在 Python 里非常有用的高阶函数.它接受一个函数和一个序列(迭代器)作为输入,然后对序列(迭代器)的每一个值应用这个函数,返回一个序列(迭代器),其包含应用函数后的结 ...

  2. python 阿狸的进阶之路(7)

    面向对象 转自林海峰的博客  http://www.cnblogs.com/linhaifeng/articles/6182264.html 面向对象的理解: 将数据分类,比如学生类.数据有关的函数, ...

  3. as2 attachMovie库影片无法获取其影片里面的对象或方法

    1.attachMovie的容器有两个,导致出错.举例子.创建了一个空的gameMc,然后容器又new个同个名字的,在这里不知道为什么不会替换,而是叠加 containerGame.createEmp ...

  4. SAFESEH 映像是不安全的

    1.打开该项目的“属性页”对话框 2.然后单击“链接器”--“命令行”,出现如下界面 3.将 /SAFESEH:NO 复制到“其它选项(D)”框中,然后点击应用 返回VS2013,重新编译运行程序即可 ...

  5. css-选择器性能

    ID选择器 比如#header 类选择器 比如.promo 元素选择器 比如 div 兄弟选择器 比如 h2 + p 子选择器 比如 li > ul 后代选择器 比如 ul a 7. 通用选择器 ...

  6. linux下svn不能连接上windows服务器:SSL handshake failed: SSL error

    在linux服务器下载https链接的svn源码时出现:SSL handshake failed: SSL error: Key usage violation in certificate has ...

  7. Kotlin语言学习笔记(2)

    类(classes) // 类声明 class Invoice { } // 空的类 class Empty // 主体构造器(primary constructor) class Person co ...

  8. python 拷贝 深拷贝 浅拷贝 赋值

    t = [1,["a","b"]] t_bak = t t_cop = copy.copy(t) t_deep = copy.deepcopy(t) print ...

  9. MVC缺点总结

    MVC的缺点: 1.完全理解MVC比较复杂. 由于MVC模式提出的时间不长,加上同学们的实践经验不足,所以完全理解并掌握MVC不是一个很容易的过程. 2.调试困难. 因为模型和视图要严格的分离,这样也 ...

  10. HttpWatch手把手图解教程

    HttpWatch手把手图解教程,提供HttpWatch下载,教您安装使用,一步到位 一 HttpWatch下载: HttpWatchProv7.2.13 破解版(带正版key) 授权:共享软件 大小 ...