INI文件读写类
INI就是扩展名为"INI"的文件,其实他本身是个文本文件,可以用记事本打工,主要存放的是用户所做的选择或系统的各种参数.
INI文件其实并不是普通的文本文件.它有自己的结构.由若干段落(SECTION)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键字(KEYWORD)和一个等号,等号右边就是关键字的值(VALUE).例如:
[Section1] KeyWord1 = Value1 KeyWord2 = Value2 ... [Section2] KeyWord3 = Value3 KeyWord4 = Value4
public class INIFile
{
public string path;
public INIFile(string INIPath)
{
path = INIPath;
}
/// <summary>
/// 写ini配置文件
/// </summary>
/// <param name="section">输入参数:段落(SECTION)</param>
/// <param name="key">输入参数:关键字(KEYWORD)</param>
/// <param name="val">输入参数:Key对应的值</param>
/// <param name="filePath">输入参数:ini文件路径</param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
/// <summary>
/// 获取ini配置文件的某Section中Key的值(
/// </summary>
/// <param name="section">输入参数:段落(SECTION)</param>
/// <param name="key">输入参数:关键字(KEYWORD)</param>
/// <param name="def"></param>
/// <param name="retVal">输入参数:获取的Key对应的值(字符串)</param>
/// <param name="size">输入参数:Key对应的值的大小</param>
/// <param name="filePath">输入参数:ini文件的路径</param>
/// <returns>返回:</returns>
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
/// <summary>
/// 获取ini配置文件的某Section中Key的值
/// </summary>
/// <param name="section">输入参数:段落(SECTION)</param>
/// <param name="key">输入参数:关键字(KEYWORD)</param>
/// <param name="defVal"></param>
/// <param name="retVal">输入参数:获取的Key对应的值(Byte[])</param>
/// <param name="size">输入参数:Key对应的值的大小</param>
/// <param name="filePath">输入参数:ini文件的路径</param>
/// <returns>返回:</returns>
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);
/// <summary>
/// 写INI文件
/// </summary>
/// <param name="Section">输入参数:段落(SECTION)</param>
/// <param name="Key">输入参数:关键字(KEYWORD)</param>
/// <param name="Value">输入参数:Key对应的值(字符串)</param>
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}
/// <summary>
/// 读取INI文件-返回字符串
/// </summary>
/// <param name="Section">输入参数:段落(SECTION)</param>
/// <param name="Key">输入参数:关键字(KEYWORD)</param>
/// <returns></returns>
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
return temp.ToString();
}
/// <summary>
/// 读取INI文件-返回Byte[]
/// </summary>
/// <param name="section">输入参数:段落(SECTION)</param>
/// <param name="key">输入参数:关键字(KEYWORD)</param>
/// <returns></returns>
public byte[] IniReadValues(string section, string key)
{
byte[] temp = new byte[255];
int i = GetPrivateProfileString(section, key, "", temp, 255, this.path);
return temp;
}
/// <summary>
/// 删除ini文件下所有段落
/// </summary>
public void ClearAllSection()
{
IniWriteValue(null, null, null);
}
/// <summary>
/// 删除ini文件下personal段落下的所有键
/// </summary>
/// <param name="Section"></param>
public void ClearSection(string Section)
{
IniWriteValue(Section, null, null);
}
}