#region 读取和写入ini文件的操作
string inipath = System.Windows.Forms.Application.StartupPath + @"config.ini";
[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="innpath">存放物理路径</param>
/// <returns></returns>
public string IniReadValue(string Section, string Key, string innpath)
{
StringBuilder temp = new StringBuilder(500);
GetPrivateProfileString(Section, Key, "", temp, 500, inipath);
return temp.ToString();
}
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
/// <summary>
/// 写入ini文件的操作
/// </summary>
/// <param name="Section">配置段</param>
/// <param name="Key">键</param>
/// <param name="Value">键值</param>
/// <param name="inipath">物理路径</param>
public void IniWriteValue(string Section, string Key, string Value,string inipath)
{
WritePrivateProfileString(Section, Key, Value, inipath);
}
#endregion
要注意的是[DllImport("kernel32")]必须要放在静态方法之前,而且不能偷懒是读和写都有一次引用。