zoukankan      html  css  js  c++  java
  • 一个很好用的配置类,通过对象系列化保存配置信息

    在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'));

  • 相关阅读:
    web开发发送短信实现最简单的接口
    2分钟学会ajax 入门ajax必备
    基于注册登陆简单的使用django认证系统
    Django 发送邮件
    关于python开始写项目创建一个虚拟环境
    pycharm使用bootstrap组件方法
    linux安装配置python环境以及虚拟环境和django下载
    luffy项目搭建流程(Django前后端分离项目范本)
    python微信服务号关注授权、消息推送流程
    Celery—分布式的异步任务处理系统
  • 原文地址:https://www.cnblogs.com/ljl_falcon/p/2420699.html
Copyright © 2011-2022 走看看