当前位置: 移动技术网 > IT编程>开发语言>c# > C# Winform 调用系统接口操作 INI 配置文件的代码

C# Winform 调用系统接口操作 INI 配置文件的代码

2019年07月18日  | 移动技术网IT编程  | 我要评论
包括了写入和读取功能. 写入的时候, 如果文件不存在会自动创建. 如果对应的键已经存在, 则自动覆盖它的值. 读取的时候, 如果对应的文件不存在, 或者键名不存在, 则返回一个 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();
}
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网