项目以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. ACM__搜素之BFS与DFS

    BFS(Breadth_First_Search) DFS(Depth_First_Search) 拿图来说 BFS过程,以1为根节点,1与2,3相连,找到了2,3,继续搜2,2与4,相连,找到了4, ...

  2. Linux:使用互斥量进行线程同步

    基础知识 同步概念 所谓同步,即同时起步,协调一致.不同的对象,对"同步"的理解方式略有不同.如,设备同步,是指在两个设备之间规定一个共同的时间参考:数据库同步,是指让两个或多个数 ...

  3. Kotlin语言编程技巧集

    空语句 Kotlin 语言中的空语句有 {} Unit when (x) { 1 -> ... 2 -> ... else -> {} // else -> Unit } Wh ...

  4. Centos7升级新内核

    由于觉得Centos7内核版本还不够高,就想升级下,下面是升级步骤 我使用的方法是使用yum升级内核 使用第三方仓库升级 CentOS 允许使用 ELRepo,这是一个第三方仓库,可以将内核升级到最新 ...

  5. avalon2学习教程06样式操作

    avalon2的ms-css的变革思路与ms-attr一样,将多个操作合并成到一个对象里面处理,因此没有ms-css-name="value",只有ms-css="Obj ...

  6. jquery实现分页+单删批删

    //定义一个分页的方法 public function fenye(){ //查询满足条件的总条数 $count = M("regis")->count(); //设置每页显 ...

  7. ANg-梯度下降算法

    概念 为了解决线性回归问题,我们也用梯度下降算法. 算法逻辑如下: 对于线性回归模型中例子,梯度下降可以如下: 算法 实际上梯度下降可有通过求导.这里的符号":="是赋值的含义 有 ...

  8. HTML+CSS基础课程二

    1.border为系统加边框<style type="text/css"> table tr td,th{border:1px solid #000;} </st ...

  9. GIS案例学习笔记-CAD数据分层导入现有模板实例教程

    GIS案例学习笔记-CAD数据分层导入现有模板实例教程 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 1. 原始数据: CAD数据 目标模板 2. 任务:分5个图层 ...

  10. Unity AssetBundle

    Unity AssetBundle爬坑手记 - 夜阑卧听风吹雨 时间 2014-09-15 16:55:00  博客园精华区原文  http://www.cnblogs.com/ybgame/p/39 ...