在winform、wpf中,需要读写config文件,本例是具体操作步骤:
相关操作已封装成类 Config.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// .net System.Configuration
using System.Configuration;
using System.Xml;
namespace CommonLib.Config
{
public class Config
{
/// <summary>
/// 配置文件名
/// </summary>
static string configPath = "Conf.exe.config";
static Configuration _config;
/// <summary>
/// 配置项集合
/// </summary>
static public Configuration config
{
get
{
if (_config == null)
{
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = configPath;
_config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
}
return _config;
}
}
/// <summary>
/// 获取.config文件中的AppSettings键值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
static public string getConfig(string key)
{
return config.AppSettings.Settings[key].Value;
}
/// <summary>
/// 设置.config文件中的APPSettings键值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
static public void setConfig(string key, object value)
{
string val = value.ToString();
config.AppSettings.Settings[key].Value = val;
config.Save();
ConfigurationManager.RefreshSection("appSettings");
}
}
}
在需要调用的地方,相关代码参考如下:
/// <summary>
/// 设备id
/// </summary>
static public string appId
{
set { Config.setConfig("appId", value); }
get { return Config.getConfig("appId"); }
}
生成的config文件格式参考如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
<appSettings>
<add key="appId" value="1"/>
<add key="dbPath" value="Prints.sqlite"/>
</appSettings>
</configuration>