c# - web.config : Changing an application setting with C#

标签 c# .net asp.net web-config

如何使用 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/

相关文章:

javascript - 我这样使用 asp mvc Controller 和 javascript 的方法是否正确?

.NET 4 GC 触发收集的已知阈值?

c# - 我想在 Mono Windows 窗体中使用 WebBrowser 控件

c# - WCF 不包括要在派生类中序列化的数据成员

c# - 将 DrawingImage 转换为 BitmapImage

c# - 如何在 Windows 窗体 (C#) 中动态添加组合框并将其绑定(bind)到 sql 数据库中表的列

c# - 如何突出显示 C# 中的特定单词?

c# - 将位图保存到流中

asp.net - 末尾接受多次 (.com) 的电子邮件 ID

C# String to DateTime 这种格式 2018-05-26T00 :00:00