如何通过代码修改web.config文件中的某个key?
How to modify a key in web.config file through the code?
如果我在 Web 应用程序的 web.config
文件中有:
<appSettings>
<add key="DD" value="567_Access"/>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
</appSettings>
我知道如何像这样读取 appSettings 部分中的数据:
string accessD = ConfigurationManager.AppSettings["DD"];
但我想知道如何修改(设置)appSettings
中的一个键的值
通过代码 ?
(我想通过对这个值的特定检查来在某些情况下停止应用程序)
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();
如果我在 Web 应用程序的 web.config
文件中有:
<appSettings>
<add key="DD" value="567_Access"/>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
</appSettings>
我知道如何像这样读取 appSettings 部分中的数据:
string accessD = ConfigurationManager.AppSettings["DD"];
但我想知道如何修改(设置)appSettings
中的一个键的值
通过代码 ?
(我想通过对这个值的特定检查来在某些情况下停止应用程序)
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();