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

unit uGlobal;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Forms, TypInfo, IniFiles;typeTConfigBase = class(TPersistent)privateFFileName: string;procedure Load(AIniFile: TIniFile); overload;procedure Save(AIniFile: TIniFile); overload;protectedfunction GetSectionName: string; virtual;procedure LoadProperty(PropInfo: PPropInfo; const Section: string; IniFile: TIniFile); virtual;procedure SaveProperty(PropInfo: PPropInfo; const Section: string; IniFile: TIniFile); virtual;publicprocedure Load(AFileName: string); overload;procedure Save(AFileName: string); overload;end;TLiveUpdate = class;TApp = class(TConfigBase)privateFAppName: string;FVersion: string;FShowMsg: Boolean;FProductID: Integer;FWindowState: TWindowState;FLiveUpdate: TLiveUpdate;protectedfunction GetSectionName: string; override;publicconstructor Create;destructor Destroy; override;publishedproperty 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)privateFAvailabled: Boolean;FAutoUpdate: Boolean;FUpdatePeriod: Integer;FLastUpdateDate: TDateTime;protectedprocedure LoadProperty(PropInfo: PPropInfo; const Section: string; IniFile: TIniFile); override;procedure SaveProperty(PropInfo: PPropInfo; const Section: string; IniFile: TIniFile); override;publicconstructor Create;publishedproperty 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;implementationvarFApp: TApp;function App: TApp;
beginif FApp = nil thenFApp := TApp.Create;Result := FApp;
end;{ TConfigBase }function TConfigBase.GetSectionName: string;
beginResult := ClassName;if Result[1] = 'T' thenResult := Copy(Result, 2, Length(Result) - 1);
end;procedure TConfigBase.Load(AFileName: string);
varIniFile: TIniFile;
beginif not FileExists(AFileName) then Exit;IniFile := TIniFile.Create(AFileName);tryLoad(IniFile);finallyIniFile.Free;end;
end;procedure TConfigBase.Load(AIniFile: TIniFile);
varI, Count: Integer;PropInfo: PPropInfo;PropList: PPropList;Section: string;
beginCount := GetTypeData(ClassInfo)^.PropCount;if Count = 0 then Exit;GetMem(PropList, Count * SizeOf(Pointer));trySection := GetSectionName;GetPropInfos(ClassInfo, PropList);for I := 0 to Count - 1 dobeginPropInfo := PropList^[I];if (PropInfo <> nil) and IsStoredProp(self, PropInfo) and (PropInfo^.SetProc <> nil) thenLoadProperty(PropInfo, Section, AIniFile);end;finallyFreeMem(PropList);end;
end;procedure TConfigBase.LoadProperty(PropInfo: PPropInfo; const Section: string;IniFile: TIniFile);
varPropType: PTypeInfo;Obj: TObject;
beginPropType := PropInfo^.PropType^;if (PropType^.Kind <> tkClass) and (not IniFile.ValueExists(Section, string(PropInfo^.Name))) then Exit;case PropType^.Kind oftkClass:beginObj := GetObjectProp(self, PropInfo);if Assigned(Obj) and (Obj is TConfigBase) thenTConfigBase(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), 0));end;
end;procedure TConfigBase.Save(AFileName: string);
varIniFile: TIniFile;
beginif AFileName = '' then Exit;IniFile := TIniFile.Create(AFileName);trytrySave(IniFile);except on E: Exception doOutputDebugString(PChar(Format('Exceptoin: save to file %s fail, err: %s', [AFileName, E.Message])));end;finallyIniFile.Free;end;
end;procedure TConfigBase.Save(AIniFile: TIniFile);
varI, Count: Integer;PropInfo: PPropInfo;PropList: PPropList;Section: string;
beginSection := GetSectionName;Count := GetTypeData(ClassInfo)^.PropCount;if Count > 0 thenbeginGetMem(PropList, Count * SizeOf(Pointer));tryGetPropInfos(ClassInfo, PropList);for I := 0 to Count - 1 dobeginPropInfo := PropList^[I];if (PropInfo <> nil) and IsStoredProp(self, PropInfo) and (PropInfo^.GetProc <> nil) thenSaveProperty(PropInfo, Section, AIniFile);end;finallyFreeMem(PropList);end;end;
end;procedure TConfigBase.SaveProperty(PropInfo: PPropInfo; const Section: string;IniFile: TIniFile);
varPropType: PTypeInfo;Obj: TObject;
constFormat_d = '%d';
beginPropType := PropInfo^.PropType^;case PropType^.Kind oftkClass:beginObj := GetObjectProp(self, PropInfo);if Assigned(Obj) and (Obj is TConfigBase) thenTConfigBase(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;
beginFFileName := ChangeFileExt(ParamStr(0), '.ini');FWindowState := wsNormal;FLiveUpdate := TLiveUpdate.Create;Load(FFileName);
end;destructor TApp.Destroy;
beginSave(FFileName);inherited Destroy;
end;function TApp.GetSectionName: string;
beginResult := 'System';
end;{ TLiveUpdate }constructor TLiveUpdate.Create;
beginFAvailabled := True;FAutoUpdate := True;FUpdatePeriod := 7;FLastUpdateDate := Now;
end;procedure TLiveUpdate.LoadProperty(PropInfo: PPropInfo; const Section: string;IniFile: TIniFile);
beginif PropInfo^.Name = 'LastUpdateDate' thenFLastUpdateDate := IniFile.ReadDateTime(Section, string(PropInfo^.Name), Now)elseinherited;
end;procedure TLiveUpdate.SaveProperty(PropInfo: PPropInfo; const Section: string;IniFile: TIniFile);
beginif PropInfo^.Name = 'LastUpdateDate' thenIniFile.WriteDateTime(Section, string(PropInfo^.Name), GetFloatProp(Self, PropInfo))elseinherited;
end;end.

使用方法:

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

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

转载于:https://www.cnblogs.com/crwy/p/9378486.html

Delphi: RTTI与ini配置文件相关推荐

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

    转自:http://hi.baidu.com/andywangcn/blog/item/10ba730f48160eeb37d122e9.html 配置文件中经常用到ini文件,在VC中其函数分别为: ...

  2. 【转载】C++读写ini配置文件GetPrivateProfileString()WritePrivateProfileString()

    配置文件中经常用到ini文件,在VC中其函数分别为: #include <Windows.h> //wince,WMobile.ppc不支持这几个函数 写入.ini文件:bool Writ ...

  3. QT中如何读写ini配置文件

    本文首发于「3D视觉工坊」知识星球. 如图1所示,我们需要在QT界面中实现手动读取参数存放的位置,那么我们该如何做呢? 方法:读取ini格式的配置文件,实现路径的写入与读取. 第一步:界面构造函数中, ...

  4. mysql 8.0找不到my.ini配置文件解决方案

    "mysql 8.0 来了,该更新版本了"   为了方便日后的工作,在这里整理了一下mysql的基本配置 新建my.ini配置文件 有些时候需要设置mysql的属性,一般的可以通过 ...

  5. 【Python教程】读写ini配置文件的详细操作

    ini文件即Initialization File初始化文件,在应用程序及框架中常作为配置文件使用,是一种静态纯文本文件,使用记事本即可编辑. 配置文件的主要功能就是存储一批变量和变量值,在ini文件 ...

  6. python读取配置文件 分段_Python3读写ini配置文件的示例

    ini文件即Initialization File初始化文件,在应用程序及框架中常作为配置文件使用,是一种静态纯文本文件,使用记事本即可编辑. 配置文件的主要功能就是存储一批变量和变量值,在ini文件 ...

  7. 技术贴]强大的DELPHI RTTI–兼谈需要了解多种开发语言

    技术贴]强大的DELPHI RTTI–兼谈需要了解多种开发语言 一月 27th, 2005 by 猛禽 风焱在<"18般武艺"?>中说到他碰上的被多种语言纠缠的问题.我 ...

  8. c读取ini配置文件_Go-INI - 超赞的Go语言INI文件操作库

    INI 文件(Initialization File)是十分常用的配置文件格式,其由节(section).键(key)和值(value)组成,编写方便,表达性强,并能实现基本的配置分组功能,被各类软件 ...

  9. python-configparser生成ini配置文件

    写入配置: 创建文件:configTest.ini import configparserconfig = configparser.ConfigParser() config['DEFAULT'] ...

最新文章

  1. python 打包 .app 运行 控制台窗口_Python打包工具
  2. java文件下载以及中文乱码解决
  3. ssm整合之五 分页以及按时间查询
  4. ComponentOne FlexGrid for WinForms 中文版快速入门(9)—过滤
  5. 12c oracle 激活_Windows运维之Windows server 2016 安装及ORACLE 12C 安装
  6. ecshop适应在php7,ecshop适应PHP7的解决方法
  7. 行人属性数据集pa100k_基于InceptionV3的多数据集联合训练的行人外观属性识别方法与流程...
  8. 7 SystemVerilog语言编写UART接收
  9. JAXB 遇到的问题
  10. 【转】关于VB中Shell及ShellExecute的总结与记录
  11. Android 颜色渲染(三) Shader颜色渲染
  12. 手机U盘制作成系统启动盘后在手机端无法识别
  13. 【python办公自动化(17)】利用python向PPT文档中写入内容(证书生成器)
  14. 搜狗主动提交url并反馈快照更新软件(含源码)
  15. SQL语句之查询进阶篇---上
  16. windows7在安装vc14的时候报错(0x80240017)
  17. python supper菱形继承
  18. 使用Latex语法快速的编辑漂亮的公式
  19. 着色器(Shader)之像素着色器
  20. 相似query(句子)聚类

热门文章

  1. 南通车管所的网址更新啦
  2. Geosoft Oasis.Montaj.v7.1.1简介
  3. PHP - 性能测试工具
  4. ADS 修改绘图单位
  5. LCD vs LED vs OLED
  6. 电脑维修中的十个笑话
  7. CleanMyMac XMac苹果电脑专属系统优化工具
  8. 如何远程登入Windows系统?
  9. linux系统下操作nandflash指令,Linux驱动之Nand Flash原理及硬件操作
  10. 解决Cadence 17.4软件无法启动,capture cis启动缓慢,打开项目缓慢,allegro 打开程序未响应(即使微软拼音切换兼容模式也无法解决的情况)