C#常用操作类库二(ini配置文件读写)
2012-03-27 21:41
756 查看
public class IniHelper
{
//文件INI名称
public 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)
{
//
// TODO: Add constructor logic here
//
Path = inipath;
}
//写INI文件
public void WriteValue(string section, string key, string value)
{
WritePrivateProfileString(section, key, value, this.Path);
}
//读取INI文件指定
public string ReadValue(string section, string key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(section, key, "", temp, 255, this.Path);
return temp.ToString();
}
/**//// <summary>
/// 验证文件是否存在
/// </summary>
/// <returns>布尔值</returns>
public bool ExistFile()
{
return File.Exists(this.Path);
}
/// <summary>
/// 删除ini文件下所有段落
/// </summary>
public void ClearAllSection()
{
WriteValue(null, null, null);
}
/// <summary>
/// 删除ini文件下personal段落下的所有键
/// </summary>
/// <param name="Section"></param>
public void ClearSection(string Section)
{
WriteValue(Section, null, null);
}
}
{
//文件INI名称
public 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)
{
//
// TODO: Add constructor logic here
//
Path = inipath;
}
//写INI文件
public void WriteValue(string section, string key, string value)
{
WritePrivateProfileString(section, key, value, this.Path);
}
//读取INI文件指定
public string ReadValue(string section, string key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(section, key, "", temp, 255, this.Path);
return temp.ToString();
}
/**//// <summary>
/// 验证文件是否存在
/// </summary>
/// <returns>布尔值</returns>
public bool ExistFile()
{
return File.Exists(this.Path);
}
/// <summary>
/// 删除ini文件下所有段落
/// </summary>
public void ClearAllSection()
{
WriteValue(null, null, null);
}
/// <summary>
/// 删除ini文件下personal段落下的所有键
/// </summary>
/// <param name="Section"></param>
public void ClearSection(string Section)
{
WriteValue(Section, null, null);
}
}
相关文章推荐
- C# 文件的一些基本操作(转)//用C#读写ini配置文件
- c# winform app.config xml 配置文件 读写操作
- Windows系统中C#读写ini配置文件的程序代码示例分享
- C#编程如何编写的自动读写配置文件.ini格式
- C# 读写 ini 配置文件
- asp.net 操作INI文件的读写,读写操作本地ini配置文件
- C#_读写ini配置文件
- C#中读写INI配置文件
- 【转载】C#.NET 读写 INI配置文件
- Delphi中读写INI配置文件操作总结
- C# 操作ini配置文件帮助类
- C# 操作.ini配置文件
- C#对ini配置文件的读写
- C# 常用文件操作 文本文件 二进制文件 读写操作
- c#读写ini配置文件示例
- C#读写INI配置文件
- C# 读写INI配置文件
- c# 配置文件App.config操作类库
- PHP 的ini配置文件的读写操作函数
- C#实现ini文件读写操作