操作.ini文件的类库,该类型文件在Winform和Webform开发中经常用到,用于存储系统配置信息等。
IniHelper.cs
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace Beasyer.Lib
...{
/**//// <summary>
/// IniInfo 的摘要说明。
/// </summary>
public class IniHelper
...{
//文件INI名称
private string _Path;

/**/////声明读写INI文件的API函数
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);

[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);

//类的构造函数,传递INI文件名
public IniHelper(string inipath)
...{
this._Path = inipath;
}
//写INI文件
public void IniWriteValue(string Section,string Key,string Value)
...{
WritePrivateProfileString(Section, Key, Value, this._Path);
}
//读取INI文件指定
public string IniReadValue(string Section, string Key)
...{
StringBuilder temp = new StringBuilder(2048);
int i = GetPrivateProfileString(Section, Key, "", temp, 2048, this._Path);
return temp.ToString();
}
}
}
例如邮箱配置文件中的内容如下:
[Email]
SendProperty =0
Title =测试邮箱标题 - 2006/06/13
Notice =测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容……
SMTPServer =
SMTPPort =25
MailBox =
则操作实例代码:
IniHelper ini=new IniHelper (Server.MapPath(".")+"Email.ini");
ini.IniWriteValue("Email","Title","标题内容");
ini.IniWriteValue("Email","Notice","内容....");

被折叠的 条评论
为什么被折叠?



