通过对象系列化保存配置信息
来源:互联网 发布:windows远程mac 编辑:程序博客网 时间:2024/05/22 15:40
一个很好用的配置类,通过对象系列化保存配置信息
在about.com上看到一个简单好用的配置类,通过它可以很方便的保存、读取程序的配置信息。
(来源:http://delphi.about.com/od/windowsshellapi/a/reader-writer.htm)
unit uSettings;
interface
uses Classes;
{$M+}
type
TCustomSettings = class
public
procedure LoadFromStream(const Stream: TStream);
procedure LoadFromFile(const FileName: string);
procedure SaveToStream(const Stream: TStream);
procedure SaveToFile(const FileName: string);
end;
implementation
uses TypInfo, Sysutils;
{ TCustomSettings }
procedure TCustomSettings.LoadFromFile(const FileName: string);
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
procedure TCustomSettings.LoadFromStream(const Stream: TStream);
var
Reader: TReader;
PropName, PropValue: string;
begin
Reader := TReader.Create(Stream, $FFF);
Stream.Position := 0;
Reader.ReadListBegin;
while not Reader.EndOfList do
begin
PropName := Reader.ReadString;
PropValue := Reader.ReadString;
SetPropValue(Self, PropName, PropValue);
end;
FreeAndNil(Reader);
end;
procedure TCustomSettings.SaveToFile(const FileName: string);
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmCreate);
try
SaveToStream(Stream);
finally
Stream.Free;
end;
end;
procedure TCustomSettings.SaveToStream(const Stream: TStream);
var
PropName, PropValue: string;
cnt: Integer;
lPropInfo: PPropInfo;
lPropCount: Integer;
lPropList: PPropList;
lPropType: PPTypeInfo;
Writer: TWriter;
begin
lPropCount := GetPropList(PTypeInfo(ClassInfo), lPropList);
Writer := TWriter.Create(Stream, $FFF);
Stream.Size := 0;
Writer.WriteListBegin;
for cnt := 0 to lPropCount - 1 do
begin
lPropInfo := lPropList^[cnt];
lPropType := lPropInfo^.PropType;
if lPropType^.Kind = tkMethod then
Continue;
PropName := lPropInfo.Name;
PropValue := GetPropValue(Self, lPropInfo^.Name);
Writer.WriteString(PropName);
Writer.WriteString(PropValue);
end;
Writer.WriteListEnd;
FreeMem(lPropList);
FreeAndNil(Writer);
end;
initialization
finalization
end.
---------------------------------------------------------------------------------------------------
使用方法:
Type
TAppCfg=Class(TCustomSettings)
private
FUserName:String;
FIntValue:Integer;
published
proeprty UserName:String Read FUserName Write FUSerName;
property IntValue:Integer Read FIntValue Write FIntValue;
end;
用法:
var AppCfg:TAppCfg;
程序启动时读取:
AppCfg:=TAppCfg.Create;
AppCfg.LoadFromFile(ExtractFilePath(ParamStr(0))+'app.cfg');
程序退出时保存:
AppCfg.SaveToFile(ExtractFilePath(ParamStr(0))+'app.cfg'));
- 通过对象系列化保存配置信息
- java将系列化后的对象保存在文件中,及反系列化 (ObjectInputStream,ObjectOutputStream)
- XML---对象系列化与反系列化
- Java对象系列化
- boost系列化c++对象
- 系列化
- C#中对象的系列化与反系列化
- cocos2dx配置信息保存
- JNI配置信息保存
- QSettings 保存配置信息
- 通过log4j保存异常信息
- 07-通过cookie保存并读取用户登录信息(jsp内置对象)
- cookie对象保存页面信息
- cookie对象保存页面信息
- 程序配置信息的保存
- 保存应用程序的配置信息
- cc2640 - snv保存配置信息
- JSON怎么对字符串进行系列化和反系列化,HTML5保存到本地文件
- Actionscript 3 自定义 matedata
- 关于 宏定义
- 一个APP的诞生
- map排序
- js遍历对象属性和值
- 通过对象系列化保存配置信息
- 思考方式
- CXF进阶篇
- JAVA集合
- spring集成jpa的三种方法
- Java如何获得文件编码格式
- C/C++函数调用约定
- c#对文件夹中的文件进行删除操作
- DB2数据库认证系列教程——IBM DB2认证考前必备