获取appSettings节点下 键值对
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="versions" value="1.0.0.0"/>
<add key="terminal" value="1"/>
</appSettings>
</configuration>
<configuration>
<appSettings>
<add key="versions" value="1.0.0.0"/>
<add key="terminal" value="1"/>
</appSettings>
</configuration>
代码如下:
System.Configuration.AppSettingsReader appSettings = new System.Configuration.AppSettingsReader();
appSettings.GetValue("versions", Type.GetType("System.String")).ToString();
appSettings.GetValue("versions", Type.GetType("System.String")).ToString();
获取Connectionstings节点
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="ConnStr" connectionString="server=.;database=dbTest;uid=sa;pwd=123456;"/>
</connectionStrings>
</configuration>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="ConnStr" connectionString="server=.;database=dbTest;uid=sa;pwd=123456;"/>
</connectionStrings>
</configuration>
代码如下:
using System.Configuration;
string con=ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
修改appSettings节点
/// <summary>
/// 修改键值
/// </summary>
/// <param name="key">健</param>
/// <param name="value">值</param>
public static void saveAppSetting(string key, string value)
{
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings[key].Value = value;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
/// 修改键值
/// </summary>
/// <param name="key">健</param>
/// <param name="value">值</param>
public static void saveAppSetting(string key, string value)
{
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings[key].Value = value;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}