IniFiles 单元内容学习(二)

    技术2024-12-06  19

      在iniFiles单元中,有三个读取ini文件的三个主要操作类TCustomIniFile、TmemIniFile和TiniFile,其中在windows系统编译下,TcustomIniFile分别为TmemIniFile和TiniFile下的基类,其中在windows系统中TmemIniFile和TIniFile这两个操作类的唯一区别在于TmenIniFile花销的存储要比TiniFile要大一些。其中TcustomIniFile有几个重要的方法

      TCustomIniFile = class(TObject)

      public

        constructor Create(const FileName: string);                                          --构造方法

        function SectionExists(const Section: string): Boolean;                         --判定section是否存在

        function ReadString(const Section, Ident, Default: string): string; virtual; abstract;

     

        //根据section和Ident读写值方法

        procedure WriteString(const Section, Ident, Value: String); virtual; abstract;

        function ReadInteger(const Section, Ident: string; Default: Longint): Longint; virtual;

        procedure WriteInteger(const Section, Ident: string; Value: Longint); virtual;

        function ReadBool(const Section, Ident: string; Default: Boolean): Boolean; virtual;

        procedure WriteBool(const Section, Ident: string; Value: Boolean); virtual;

        function ReadBinaryStream(const Section, Name: string; Value: TStream): Integer; virtual;

        function ReadDate(const Section, Name: string; Default: TDateTime): TDateTime; virtual;

        function ReadDateTime(const Section, Name: string; Default: TDateTime): TDateTime; virtual;

        function ReadFloat(const Section, Name: string; Default: Double): Double; virtual;

        function ReadTime(const Section, Name: string; Default: TDateTime): TDateTime; virtual;

        procedure WriteBinaryStream(const Section, Name: string; Value: TStream); virtual;

        procedure WriteDate(const Section, Name: string; Value: TDateTime); virtual;

        procedure WriteDateTime(const Section, Name: string; Value: TDateTime); virtual;

        procedure WriteFloat(const Section, Name: string; Value: Double); virtual;

        procedure WriteTime(const Section, Name: string; Value: TDateTime); virtual;

     

        //读写section内容

        procedure ReadSection(const Section: string; Strings: TStrings); virtual; abstract;

        procedure ReadSections(Strings: TStrings); virtual; abstract;

        procedure ReadSectionValues(const Section: string; Strings: TStrings); virtual; abstract;

        procedure EraseSection(const Section: string); virtual; abstract;

     

        //删除section对应下的Ident

        procedure DeleteKey(const Section, Ident: String); virtual; abstract;

     

        //根据缓存更新相应的文件

        procedure UpdateFile; virtual; abstract;

     

        function ValueExists(const Section, Ident: string): Boolean;

        property FileName: string read FFileName;

      end;

     

       其中值得注意的是,在window下TiniFile 或者在Linux下TMemInifile中write相关的方法,如WriteData、WriteString等做了容错处理,如果对应的.ini文件不存在相关的section或者key的话,会创建相应的section并且写入相应的key值,并且给该key值初始化。

     

      procedure TForm1.Button2Click(Sender: TObject);

      var

         IniFile: TIniFile;

       begin

         IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'config.ini');

         //直接调用writestring函数 如果该section或者key不存在 该函数会创建并且初始化该值

         IniFile.WriteString('REPORTPRINTHEIGHT', 'ReportHeardHeightAdd', '0');

       end;

    最新回复(0)