在运行时更新 App.config 里的 AppSettings 属性及自定义配置节

发表信息: by Creative Commons Licence

本文展示了如何在运行时更新(添加,修改或删除)App.config 里的 AppSettings 属性及自定义配置节。

App.config 文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="geoSettings">
            <section name="summary" type="System.Configuration.NameValueSectionHandler" />
        </sectionGroup>
    </configSections>

    <appSettings>
        <add key="Language" value="Ruby" />
        <add key="Version" value="1.9.3" />
    </appSettings>

    <geoSettings>
        <summary>
            <add key="Country" value="New Zealand" />
            <add key="City" value="Christchurch" />
        </summary>
    </geoSettings>
</configuration>

更新 AppSettings

添加一个元素

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("OS", "Linux");
config.Save(ConfigurationSaveMode.Modified);

ConfigurationManager.RefreshSection("appSettings");

修改已有元素的键值

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["Version"].Value = "2.0.0";
config.Save(ConfigurationSaveMode.Modified);

ConfigurationManager.RefreshSection("appSettings");

删除已有的元素

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove("Version");
config.Save(ConfigurationSaveMode.Modified);

ConfigurationManager.RefreshSection("appSettings");

更新自定义配置节

添加一个元素

var xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

// create new node <add key="Region" value="Canterbury" />
var nodeRegion = xmlDoc.CreateElement("add");
nodeRegion.SetAttribute("key", "Region");
nodeRegion.SetAttribute("value", "Canterbury");

xmlDoc.SelectSingleNode("//geoSettings/summary").AppendChild(nodeRegion);
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

ConfigurationManager.RefreshSection("geoSettings/summary");

修改已有元素的键值

var xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

xmlDoc.SelectSingleNode("//geoSettings/summary/add[@key='Country']").Attributes["value"].Value = "Old Zeeland";
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

ConfigurationManager.RefreshSection("geoSettings/summary");

删除已有的元素

var xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

XmlNode nodeCity = xmlDoc.SelectSingleNode("//geoSettings/summary/add[@key='City']");
nodeCity.ParentNode.RemoveChild(nodeCity);

xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
ConfigurationManager.RefreshSection("geoSettings/summary");
NameValueCollection appSettings = ConfigurationManager.AppSettings;
// var customSettings = ConfigurationManager.GetSection("geoSettings/summary") as NameValueCollection;

foreach (string key in appSettings.AllKeys) {
    Console.WriteLine("{0}: {1}", key, section[key]);
}

参考文献

  1. Modifying app.config at runtime throws exception
  2. update app.config file programatically with ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  3. Opening the machine/base Web.Config (64bit) through code