使用C#操作ini文件

原作:BLaZiNiX         翻译: dragontt<?XML:NAMESPACE PREFIX = O />


      这个类,封装了Kernal32.dll 中提供的方法来操作ini文件。

简介:

     这里创建了一个类,封装了KERNEL32.dll中提供的两个方法,用来操作ini文件。这两个方法是:WritePrivateProfileString GetPrivateProfileString

  需要引用的命名空间为:System.Runtime.InteropServices System.Text

类源文件

None.gif using  System;
None.gif
using  System.Runtime.InteropServices;
None.gif
using  System.Text;
None.gif
namespace  Ini
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// &lt;summary&gt;
InBlock.gif   
/// Create a New INI file to store or load data
ExpandedSubBlockEnd.gif   
/// &lt;/summary&gt;

InBlock.gif   public class IniFile
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif
InBlock.gif       
public string path;
InBlock.gif
InBlock.gif       [DllImport(
"kernel32")]
InBlock.gif       
private static extern long WritePrivateProfileString(string section, string key,string val,string filePath);
InBlock.gif
InBlock.gif       [DllImport(
"kernel32")]
InBlock.gif      
private static extern int GetPrivateProfileString(string section, string key,string def, StringBuilder retVal,
InBlock.gif           
int size,string filePath);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// &lt;summary&gt;
InBlock.gif       
/// INIFile Constructor.
InBlock.gif       
/// &lt;/summary&gt;
ExpandedSubBlockEnd.gif      
/// &lt;PARAM name="INIPath"&gt;&lt;/PARAM&gt;

InBlock.gif       public IniFile(string INIPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif           path 
= INIPath;
ExpandedSubBlockEnd.gif       }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// &lt;summary&gt;
InBlock.gif       
/// Write Data to the INI File
InBlock.gif       
/// &lt;/summary&gt;
InBlock.gif       
/// &lt;PARAM name="Section"&gt;&lt;/PARAM&gt;
InBlock.gif       
/// Section name
InBlock.gif       
/// &lt;PARAM name="Key"&gt;&lt;/PARAM&gt;
InBlock.gif       
/// Key Name
InBlock.gif       
/// &lt;PARAM name="value"&gt;&lt;/PARAM&gt;
ExpandedSubBlockEnd.gif       
/// value Name

InBlock.gif       public void IniWritevalue(string Section,string Key,string value)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif           WritePrivateProfileString(Section,Key,value,
this.path);
ExpandedSubBlockEnd.gif       }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// &lt;summary&gt;
InBlock.gif       
/// Read Data value From the Ini File
InBlock.gif       
/// &lt;/summary&gt;
InBlock.gif       
/// &lt;PARAM name="Section"&gt;&lt;/PARAM&gt;
InBlock.gif       
/// &lt;PARAM name="Key"&gt;&lt;/PARAM&gt;
InBlock.gif       
/// &lt;PARAM name="Path"&gt;&lt;/PARAM&gt;
ExpandedSubBlockEnd.gif       
/// &lt;returns&gt;&lt;/returns&gt;

InBlock.gif       public string IniReadvalue(string Section,string Key)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif           StringBuilder temp 
= new StringBuilder(255);
InBlock.gif
InBlock.gif           
int i = GetPrivateProfileString(Section,Key,"",temp, 255this.path);
InBlock.gif           
return temp.ToString();
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

None.gif

使用这个类


按照下列步骤使用:


1.              在你的项目中加入命名空间的引用

using INI;

2.              创建一个如下的INIFile对象

INIFile ini = new INIFile("C:\\test.ini");

3.              使用IniWritevalue方法在指定的配置节给一个键付值,或者使用IniReadvalue方法在指定的一个配置节中读取某个键的值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值