如何使用 C# 以编程方式更改 web.copnfig 中的应用程序设置(从另一个配置 Web 应用程序的应用程序)? 以下代码片段不起作用,因为 AppSettings[...] 是只读!
configuration = WebConfigurationManager.OpenWebConfiguration(...);
ConfigurationSectionGroup configurationSectionGroup = (ConfigurationSectionGroup)configuration.GetSectionGroup("applicationSettings");
ConfigurationSection configurationSection = (ConfigurationSection)configurationSectionGroup.Sections[...];
configurationSection.CurrentConfiguration.AppSettings[...].value = value
最佳答案
您可以更改app.config。我通过将其作为 XML 文档加载并更改其节点来完成此操作。我认为 webюconfig 也可以这样做。
这是如何使用 XML 读取 web.config 的示例,但可以对其进行一些更改以将其用于写入: http://dotnetacademy.blogspot.com/2010/10/read-config-file-using-xml-reader.html
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("~/") + "app.config");
XmlNode node = xmlDoc.SelectSingleNode("Root/Node/Element");
node.Attributes[0].Value = newValue;
xmlDoc.Save(xmlFile);
下面的链接提供了一个如何更改 xml 节点值的好示例: http://www.fryan0911.com/2009/10/change-xml-file-node-value-using-c.html
关于c# - web.config : Changing an application setting with C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4050649/