如何在C#中读写INI文件

2024-12-03 04:28:13
推荐回答(1个)
回答1:

public static class SIIniFile
{

[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string strSection, string strKey, string strVal, string strFilePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string strSection, string strKey, string strDef, StringBuilder retVal, int intSize, string strfilePath);

//对于系统本地参数使用这两个函数读取,如果将来使用XML或其它类型取代Ini时只需要重写这两个函数.
///


/// 写ini文件操作的函数,原GetProfileString函数.
///

/// 功能分组
/// 属性
///
/// 文件路径
public static long SetLocalSysParam(string section, string key, string value, string filePath)
{
return WritePrivateProfileString(section, key, value, filePath);
}

///
/// 读ini文件操作的函数,原ProflieString函数.
///

/// 功能分组
/// 属性
/// 默认值
/// 文件路径
///
public static string GetLocalSysParam(string section, string key, string def, string filePath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(section, key, def, temp, 255, filePath);
return temp.ToString();
}
}