封装 INI 文件读写函数!

本文介绍了一种简化Ini文件操作的方法,通过封装读写函数来处理不同数据类型,实现更便捷的数据访问。提供了实例代码演示如何使用封装后的函数进行Ini文件的读取与写入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在实际应用,每次读写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);
ReadIniValue('c:\中国烂鞋.ini','锅家队','猪教练',stString,'郭12');
ReadIniValue('c:\中国烂鞋.ini','锅家队','平均罚球',stInt,-1);




        
        



        







        
          
            
            评论这张
          
        


          
            
               封装 INI 文件读写函数! - yyimen - yyimen的博客
            
            转发至微博
          
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值