C#中实现读写INI文件中的值
/**/////声明读写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);
/**//// <summary>
/// 写INI文件
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Value"></param>
public static void IniWriteValue(string path, string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, path);
}
/**//// <summary>
/// 读取INI文件指定
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <returns></returns>
public static string IniReadValue(string path, string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
return temp.ToString();
}
[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);
/**//// <summary>
/// 写INI文件
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Value"></param>
public static void IniWriteValue(string path, string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, path);
}
/**//// <summary>
/// 读取INI文件指定
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <returns></returns>
public static string IniReadValue(string path, string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
return temp.ToString();
}
来源:https://www.cnblogs.com/jetxia/archive/2007/04/03/698123.html