打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
封装 INI 文件读写函数!

封装 INI 文件读写函数!

在实际应用,每次读写Ini文件,都要引用Ini文件,还要区分数据类型调用不同的Ini函数,这实在是有点烦!

为了方便,我特地将Ini用到的函数封装成两个函数。

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;

调用范例:

WriteIniValue('c:\中国烂鞋.ini','锅家队','猪教练','郭十二',stString);

WriteIniValue('c:\中国烂鞋.ini','锅家队','平均罚球',1,stInt);

WriteIniValue('c:\中国烂鞋.ini','锅家队','日期',date,stdate);

 

ReadIniValue('c:\中国烂鞋.ini','锅家队','猪教练',stString,'郭12');

ReadIniValue('c:\中国烂鞋.ini','锅家队','平均罚球',stInt,-1);

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#操作INI文件(调用WindowsAPI函数WritePrivateProfileString,GetPrivateProfileString)
C#读INI文件
Ini文件操作类
IniFile.cs:C#来操作ini配置文件
step by step 之餐饮管理系统五(Util模块)
Qt读写ini文件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服