您现在的位置:首页 >> 基础算法 >> window基础 >> 内容

Delphi中实现封装INI文件读写函数

时间:2011/9/3 15:29:20 点击:

  核心提示:uses IniFiles;//简单数据类型TSimpleType = (stInt, stFloat, stString, stDateTime, stDate, stTime, stBoolean...

uses IniFiles;

//简单数据类型
TSimpleType = (stInt, stFloat, stString, stDateTime, stDate, stTime, stBoolean);

   /// <summary>
    /// 读Ini文件的函数
    /// </summary>
    /// <param name="FileName">Ini文件名</param>
    /// <param name="Section">节点</param>
    /// <param name="Name">字段名</param>
    /// <param name="SimpleType">简单数据类型</param>
    /// <param name="DefaultValue">默认值</param>
    /// <returns>返回变体类型</returns>

function ReadIniValue(const FileName, Section, Name: string;
      SimpleType: TSimpleType; DefaultValue: Variant): Variant;

begin

   with TIniFile.Create(FileName) do
try
    if SimpleType = stString then
      Result := ReadString(Section, Name, DefaultValue)
    else if SimpleType = stInt then
      Result := ReadInteger(Section, Name, DefaultValue)
    else if SimpleType = stFloat then
      Result := ReadFloat(Section, Name, DefaultValue)
    else if SimpleType = stDateTime then
      Result := ReadDateTime(Section, Name, DefaultValue)
    else if SimpleType = stDate then
      Result := ReadDate(Section, Name, DefaultValue)
    else if SimpleType = stTime then
      Result := ReadTime(Section, Name, DefaultValue)
    else if SimpleType = stBoolean then
      Result := ReadBool(Section, Name, DefaultValue);
finally
    Free;
end;

    /// <summary>
    /// 写INI文件的函数
    /// </summary>
    /// <param name="FileName">ini文件名</param>
    /// <param name="Section">节点名</param>
    /// <param name="Name">字段名</param>
    /// <param name="Value">字段值</param>
    /// <param name="SimpleType">简单类型</param>   

procedure WriteIniValue(const FileName, Section, Name: string;
      Value: Variant; SimpleType: TSimpleType);
begin
with TIniFile.Create(FileName) do
try
    if SimpleType = stString then
      WriteString(Section, Name, VarToStr(Value))
    else if SimpleType = stInt then
      WriteInteger(Section, Name, Value)
    else if SimpleType = stFloat then
      WriteFloat(Section, Name, Value)
    else if SimpleType = stDateTime then
      WriteDateTime(Section, Name, VarToDateTime(Value))
    else if SimpleType = stDate then
      WriteDate(Section, Name, VarToDateTime(Value))
    else if SimpleType = stTime then
      WriteTime(Section, Name, VarToDateTime(Value))
    else if SimpleType = stBoolean then
      WriteBool(Section, Name, Value);
finally
    Free;
end;

end;

作者:碧水航 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 盒子文章(www.2ccc.com) © 2022 版权所有 All Rights Reserved.
  • 沪ICP备05001939号