现假设您有一个WebApplication名叫MyWebApp,虚拟路径为:http://localhost/MyWebApp,目录路径为:c:\inetpub\wwwroot\MyWebApp,其中配置文件web.config中的appSettings设置如下:<configuration>
 <appSettings>
     <add key="test" value="runtime modify test"/>
 </appSettings></configuration>创建一个新的Console应用程序加入以下代码:using System;
using System.Management;namespace ConsoleApplication1
{
 class Class1
 {
  [STAThread]
  static void Main(string[] args)
  {
   ManagementObject appSet= new ManagementObject("root\\NetFrameworkV1:appSettings.Directive=\"add\",key=\"test\",Selector=\"file://C:/Inetpub/wwwroot/MyWebApp/web.config\"");
   Console.WriteLine(string.Format("Old value:{0}",appSet["value"]));
   appSet.SetPropertyValue("value","new value was set ok!");
   appSet.Put();
   Console.WriteLine("New value setted ok!");
   Console.Read();
  }
 }
}编译并运行,然后查看MyWebApp/web.config是否已被成功修改.
不知道对不对!

解决方案 »

  1.   

    see:
    Runtime Web.config / App.config Editing 
    http://www.eggheadcafe.com/articles/20030907.asp
      

  2.   

    aspx
    --------------------
    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="XML_.WebForm1" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <FONT face="宋体">
    <asp:Button id="button1" style="Z-INDEX: 101; LEFT: 384px; POSITION: absolute; TOP: 216px" runat="server"
    Text="Button"></asp:Button>
    <asp:TextBox id="txtUserName" style="Z-INDEX: 102; LEFT: 200px; POSITION: absolute; TOP: 96px"
    runat="server"></asp:TextBox>
    <asp:TextBox id="txtPassword" style="Z-INDEX: 103; LEFT: 208px; POSITION: absolute; TOP: 144px"
    runat="server"></asp:TextBox></FONT>
    </form>
    </body>
    </HTML>
    ===================================
    cs
    ------------------
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Xml;
    using System.Configuration;namespace XML_
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox txtUserName;
    protected System.Web.UI.WebControls.TextBox txtPassword;
    protected System.Web.UI.WebControls.Button button1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.button1.Click += new System.EventHandler(this.button1_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void button1_Click(object sender, System.EventArgs e)
    {
    XmlDocument xDoc = new XmlDocument();
    xDoc.Load(MapPath("")+"//Web.config");

    XmlNode xNode;
    XmlElement xElem1;
    XmlElement xElem2; xNode =  xDoc.SelectSingleNode("//appSettings");

    xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='UserName']");
    if ( xElem1 != null ) xElem1.SetAttribute("value",this.txtUserName.Text);
    else
    {
    xElem2 = xDoc.CreateElement("add");
    xElem2.SetAttribute("key","UserName");
    xElem2.SetAttribute("value",this.txtUserName.Text);
    xNode.AppendChild(xElem2);
    }
       
    xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='Password']");
    if ( xElem1 != null ) xElem1.SetAttribute("value",this.txtPassword.Text);
    else
    {
    xElem2 = xDoc.CreateElement("add");
    xElem2.SetAttribute("key","Password");
    xElem2.SetAttribute("value",this.txtPassword.Text);
    xNode.AppendChild(xElem2);
    }
    xDoc.Save(MapPath("")+"//Web.config"); this.button1.Enabled = false;

    }
    }
    }