我知道我可以做如下的事情,以便将键添加到应用程序设置部分:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("OS", "Linux");
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
但我想在节点外添加另一个节点<appSettings>
所以它看起来像下面这样:
<appSettings>
</appSettings>
<myCustomSetting firstValue="value1" secondValue="value2"/>
如何在 c# 中执行此操作?
你可以
这样做,
var xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
var nodeRegion = xmlDoc.CreateElement("myCustomSetting ");
nodeRegion.SetAttribute("firstValue", "value1");
nodeRegion.SetAttribute("secondValue", "value2");
xmlDoc.SelectSingleNode("//myCustomSetting").AppendChild(nodeRegion);
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
你可以在这里红Update custom configuration sections