Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  Asp.net  >  正文 c# Winform 操作INI配置文件的代码

c# Winform 操作INI配置文件的代码

发布时间:2016-01-26   编辑:www.jquerycn.cn
c#封闭的一小段代码,可以调用系统接口,操作配置文件。多用于 .ini 文件,或其它键值对格式的配置文件。

以下代码实现了写入和读取功能:
写入时,如果文件不存在会自动创建。如果对应的键已经存在, 则自动覆盖它的值。
读取时,如果对应的文件不存在,或者键名不存在,则返回一个 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 配置文件的实现代码
c# Winform 操作INI配置文件的代码
如何在C#中读写INI文件
c# api读写ini配置文件的类
php读取与修改自定义配置文件的代码
php操作ini配置文件的例子
如何进行python的ini文件修改?
构建可配置PHP应用程序的正确方式
Vs2005之简单日志工具的制作--2.根据功能分析进行系统设计
asp.net WinForm TextBox事件的组合代码

[关闭]