C# Winform 调用系统接口操作 INI 配置文件的代码
C# Winform 调用系统接口操作 INI 配置文件的代码
发布时间:2016-12-28 来源:查字典编辑
摘要:包括了写入和读取功能.写入的时候,如果文件不存在会自动创建.如果对应的键已经存在,则自动覆盖它的值.读取的时候,如果对应的文件不存在,或者键...

包括了写入和读取功能. 写入的时候, 如果文件不存在会自动创建. 如果对应的键已经存在, 则自动覆盖它的值. 读取的时候, 如果对应的文件不存在, 或者键名不存在, 则返回一个 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#教程学习
热门C#教程学习
编程开发子分类