C# 读写INI文件

时间:2021-07-07 04:27:14
#region INI 读取写入
[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);

private string iniFilePath = Application.StartupPath + "\\public.ini";

/// <summary>
/// 写入INI
/// </summary>
private void IniWrite(string section, string key, string value)
{
	WritePrivateProfileString(section, key, value, iniFilePath);
}

/// <summary>
/// 获取ini
/// </summary>
private string IniRead(string section, string key)
{
	StringBuilder temp = new StringBuilder(255);
	int i = GetPrivateProfileString(section, key, "", temp, 255, iniFilePath);
	return temp.ToString();
}
#endregion