找到你要的答案

Q:Is there any way to update the Web.config file in asp.net through .cs or .aspx page [duplicate]

Q:有什么方法来更新Web.config文件在ASP.NET通过CS或.aspx页[复制]

This question already has an answer here:

I need to change the value appSettings from the web.config file in ASP.Net through the .aspx or .cs page. Is it possible> Please provide some example, my sample code is:

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="variable" value="7" />
    <add key="logfilelocation" value="abc.txt" />
  </appSettings>
</configuration>

这个问题在这里已经有了答案:

我需要改变的值从ASP中的web.config文件里。网通过.aspx或CS页。它是可能的>;请提供一些例子,我的示例代码:

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="variable" value="7" />
    <add key="logfilelocation" value="abc.txt" />
  </appSettings>
</configuration>
answer1: 回答1:

Based on this thread

 Configuration myConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
  myConfiguration.AppSettings.Settings.Item("myKey").Value = txtmyKey.Text;
  myConfiguration.AppSettings.Settings.Remove("MyVariable");
  myConfiguration.AppSettings.Settings.Add("MyVariable", "MyValue");
  myConfiguration.Save();

基于此线程

 Configuration myConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
  myConfiguration.AppSettings.Settings.Item("myKey").Value = txtmyKey.Text;
  myConfiguration.AppSettings.Settings.Remove("MyVariable");
  myConfiguration.AppSettings.Settings.Add("MyVariable", "MyValue");
  myConfiguration.Save();
answer2: 回答2:

Yes it's possible but should be done with caution. The example I've had to do this in is as part of an installer project, so the web.config wasn't in use and the app was being installed for the first time.

Something like this could be adapted for your purpose:

private string ConfigPath { get { return Path.Combine(targetDirectory.Substring(0, targetDirectory.LastIndexOf("\\") - 1), "Web.Config"); } }

XmlDocument doc = new XmlDocument();
doc.Load(this.ConfigPath);

XmlNode MyNode = doc.SelectSingleNode("configuration/appSettings/add[@key='YourKey']");
MyNode.Attributes["value"].Value = YourValue;

doc.Save(this.ConfigPath);

Alternatively you can use the ConfigurationManager like this to modify an existing key:

var config = System.Web.Configuration.WebConfigurationManager
      .OpenWebConfiguration("~/web.config");
config.AppSettings.Settings["Your Key"].Value = "Your Value";
config.Save(ConfigurationSaveMode.Modified);

是的,这是可能的,但应该谨慎行事。我不得不做这个例子中是作为一个安装项目的一部分,所以一些不在使用的应用程序被安装的第一时间。

这样的东西可以适合你的目的:

private string ConfigPath { get { return Path.Combine(targetDirectory.Substring(0, targetDirectory.LastIndexOf("\\") - 1), "Web.Config"); } }

XmlDocument doc = new XmlDocument();
doc.Load(this.ConfigPath);

XmlNode MyNode = doc.SelectSingleNode("configuration/appSettings/add[@key='YourKey']");
MyNode.Attributes["value"].Value = YourValue;

doc.Save(this.ConfigPath);

或者你可以使用ConfigurationManager这样修改现有的关键:

var config = System.Web.Configuration.WebConfigurationManager
      .OpenWebConfiguration("~/web.config");
config.AppSettings.Settings["Your Key"].Value = "Your Value";
config.Save(ConfigurationSaveMode.Modified);
c#  asp.net  web-config