如何在C#中读写INI文件
1个回答
2017-09-20
展开全部
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时只需要重写这两个函数.
/// <summary>
/// 写ini文件操作的函数,原GetProfileString函数.
/// </summary>
/// <param name="section">功能分组</param>
/// <param name="key">属性</param>
/// <param name="value">值</param>
/// <param name="filePath">文件路径</param>
public static long SetLocalSysParam(string section, string key, string value, string filePath)
{
return WritePrivateProfileString(section, key, value, filePath);
}
/// <summary>
/// 读ini文件操作的函数,原ProflieString函数.
/// </summary>
/// <param name="section">功能分组</param>
/// <param name="key">属性</param>
/// <param name="def">默认值</param>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
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();
}
}
{
[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时只需要重写这两个函数.
/// <summary>
/// 写ini文件操作的函数,原GetProfileString函数.
/// </summary>
/// <param name="section">功能分组</param>
/// <param name="key">属性</param>
/// <param name="value">值</param>
/// <param name="filePath">文件路径</param>
public static long SetLocalSysParam(string section, string key, string value, string filePath)
{
return WritePrivateProfileString(section, key, value, filePath);
}
/// <summary>
/// 读ini文件操作的函数,原ProflieString函数.
/// </summary>
/// <param name="section">功能分组</param>
/// <param name="key">属性</param>
/// <param name="def">默认值</param>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询