【问题标题】:How to modify a key in web.config file through the code? [duplicate]如何通过代码修改web.config文件中的key? [复制]
【发布时间】:2016-12-23 13:26:00
【问题描述】:

如果我在 web 应用程序的 web.config 文件中有:

<appSettings>
    <add key="DD" value="567_Access"/>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
</appSettings>

我知道如何读取 appSettings 部分中的数据,如下所示:

 string accessD = ConfigurationManager.AppSettings["DD"];

但我想知道如何修改(设置)appSettings 中某个键的值 通过代码?

(我想通过对这个值的特定检查在某些情况下停止应用程序)

【问题讨论】:

标签: c# asp.net xml iis web-config


【解决方案1】:
System.Configuration.ExeConfigurationFileMap configFile = new System.Configuration.ExeConfigurationFileMap();
configFile.ExeConfigFilename = "ConsoleTester.exe.config";  //name of your config file, can be from your app or external
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configFile, System.Configuration.ConfigurationUserLevel.None);
System.Configuration.KeyValueConfigurationCollection settings = config.AppSettings.Settings;
settings["DD"].Value = "007_Access";
config.Save();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 2013-02-15
    相关资源
    最近更新 更多