如:
 </system.web>
 <appSettings>
     <add key="strConn" value="server=back10.dhcn.net;database=telesales_net;user=sa;"/>
     <add key="regno" value="213333333324949376"/>
   </appSettings></configuration>读时这样可以:string regno=ConfigurationSettings.AppSettings["regno"].Trim();现在我想把"regno" 的value 改为另一个数字,如何实现呢?

解决方案 »

  1.   

    给你贴个网站,可能对你有帮助
    http://news.dvbbs.net/infoview/Article_2627.html
      

  2.   

    在项目运行时修改web.config会使Session全部丢失,所以你的想法是不现实的,应该换一种方法实现你要的功能.
      

  3.   

    给你一个修改的示例程序<%@ Page language="c#" Codebehind="ReadWriteConfig.aspx.cs" AutoEventWireup="false" Inherits="Writeconfig.WebForm1" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <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">
    <asp:DropDownList id="DropDownList1" style="Z-INDEX: 101; LEFT: 344px; POSITION: absolute; TOP: 136px" runat="server"></asp:DropDownList>
    <asp:TextBox id="TextBox1" style="Z-INDEX: 102; LEFT: 344px; POSITION: absolute; TOP: 88px" runat="server"></asp:TextBox>
    <asp:Button id="Writebtn" style="Z-INDEX: 103; LEFT: 192px; POSITION: absolute; TOP: 136px" runat="server" Text="修改Web.config"></asp:Button>
    <asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 208px; POSITION: absolute; TOP: 96px" runat="server" Font-Size="X-Small">替代当前列表选项的值:</asp:Label>
    </form>
    </body>
    </HTML>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;
    namespace Writeconfig
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DropDownList DropDownList1;
    protected System.Web.UI.WebControls.Button Writebtn;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.TextBox TextBox1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!Page.IsPostBack)
    {
    read();
    }
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Writebtn.Click += new System.EventHandler(this.Writebtn_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Writebtn_Click(object sender, System.EventArgs e)
    {
    write();
    read();
    } public void read()
    {
    string filename=Server.MapPath("") + @"\Web.config";
    XmlDocument  xmldoc= new XmlDocument();
    xmldoc.Load(filename); XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
    foreach(XmlElement element in topM)
    {
    if(element.Name=="appSettings")
    {
    XmlNodeList node=element.ChildNodes;
    if ( node.Count > 0 )
    {
    DropDownList1.Items.Clear();
    foreach(XmlElement el in node)
    {
    DropDownList1.Items.Add(el.Attributes["key"].InnerXml);
    }
    }
    }
    }
    } public void write()
    {
    string filename=Server.MapPath("") + @"\Web.config";
    XmlDocument  xmldoc= new XmlDocument();
    xmldoc.Load(filename); XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
    foreach(XmlElement element in topM)
    {
    if(element.Name=="appSettings")
    {
    XmlNodeList node=element.ChildNodes;
    if ( node.Count > 0 ) 
    {
    foreach(XmlElement el in node)
    {
    if(el.Attributes["key"].InnerXml.ToLower()==this.DropDownList1.SelectedItem.Value.ToLower())
    {
    el.Attributes["key"].Value=this.TextBox1.Text;
    }
    }
    }
    }
    }
    xmldoc.Save(filename);
    }
    }
    }
      

  4.   

    可以修改try public static bool appSettingsEdit(string WebConfigDirectory,string appSettingsAddkey,string keyvalue)
      {
       try
       {
        string path=WebConfigDirectory+"\\web.config";
        XmlDocument xd=new XmlDocument();
        xd.Load(path);    //如果没有appSetting,则添加
        if(xd.SelectNodes("//appSettings").Count==0)
        {
         xd.DocumentElement.AppendChild(xd.CreateElement("appSettings"));
        }    //判断节点是否存在,如果存在则修改当前节点
        bool addNode=true;
        foreach(XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add"))
        {
         if(xn1.Attributes["key"].Value==appSettingsAddkey)
         {
          addNode=false;
          xn1.Attributes["value"].Value=keyvalue;
    //      xn1.ParentNode.RemoveChild(xn1);
          break;
         }
        }    //当前节点不存在,则添加新节点
        if(addNode)
        {
         //创建新节点
         XmlNode xn2=xd.CreateElement("add");     //添加key
         XmlAttribute xa=xd.CreateAttribute("key");
         xa.Value=appSettingsAddkey;
         xn2.Attributes.Append(xa);     //添加value
         xa=xd.CreateAttribute("value");
         xa.Value=keyvalue;
         xn2.Attributes.Append(xa);
         xd.SelectSingleNode("/configuration/appSettings").AppendChild(xn2);
        }
        //保存web.config
        xd.Save(path);
        return true;
       }
       catch
       {
        return false;
       }
      }
      

  5.   

    public string readWebConfig(string KeyName)
    {
        string sDataSource = System.Configuration.ConfigurationSettings.AppSettings.Get(KeyValue);
                return sDataSource;
    } public string writeWebConfig(string KeyName,string KeyValue)
    {
        System.Configuration.ConfigurationSettings.AppSettings.Set(KeyName,KeyValue);
    }
      

  6.   

    请问一下,我用
    System.Configuration.ConfigurationSettings.AppSettings.Set(KeyName,KeyValue);
    出错!“集合是只读的”
    什么意思!!!我已经把web.config的权限里面+了个asp.net的用户,用户有所有权限。
    还有一个问题
    比如我的web.config里面
    <appSettings>
    <add key="RegName" value="444"/>
    <add key="RegCode" value="222"/>
    <add key="Phone" value="138000000"/>
    <add key="PhonePass" value="123456"/>

    </appSettings>
    我用    LaoDai_Net(老代.Net『学无止境』)   提供的代码
    xn1.Attributes["key"].Value这个值可以得到RegName
    我应该怎么把RegName 的value “444”改成“555”呢 ??
    谢谢大家指教一下