包括了写入和读取功能. 写入的时候, 如果文件不存在会自动创建. 如果对应的键已经存在, 则自动覆盖它的值. 读取的时候, 如果对应的文件不存在, 或者键名不存在, 则返回一个 empty 值. 非常方便 ^_^
实现代码如下:

// 系统接口类
public static class WinAPI
{
[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);

// 向配置文件写入值
public static void ProfileWriteValue(
string section, string key, string value, string path)
{
WritePrivateProfileString(section, key, value, path);
}

// 读取配置文件的值
public static string ProfileReadValue(
string section, string key, string path)
{
StringBuilder sb = new StringBuilder(255);
GetPrivateProfileString(section, key, "", sb, 255, path);
return sb.ToString().Trim();
}
}

以上就是【C# Winform 调用系统接口操作 INI 配置文件的代码】的全部内容了,欢迎留言评论进行交流!

赞(0) 踩(0)
发表我的评论

最新评论

  1. 暂无评论