如何在程式中修改web.config 中的内容?

yesterdaycsdn 2005-05-27 12:08:12


如:
</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 改为另一个数字,如何实现呢?
...全文
358 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhady 2005-06-16
  • 打赏
  • 举报
回复
请问一下,我用
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”呢 ??
谢谢大家指教一下
Oceanson 2005-06-16
  • 打赏
  • 举报
回复
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);
}
dicklee1214 2005-06-01
  • 打赏
  • 举报
回复
关注ing。。。。。
老戴12345654 2005-06-01
  • 打赏
  • 举报
回复
可以修改

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;
}
}


rockrabbit 2005-06-01
  • 打赏
  • 举报
回复
很好,正是我要用的。
hchxxzx 2005-05-27
  • 打赏
  • 举报
回复
给你一个修改的示例程序

<%@ 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);
}
}
}
coldpanth 2005-05-27
  • 打赏
  • 举报
回复
在项目运行时修改web.config会使Session全部丢失,所以你的想法是不现实的,应该换一种方法实现你要的功能.
daimi01171 2005-05-27
  • 打赏
  • 举报
回复
给你贴个网站,可能对你有帮助
http://news.dvbbs.net/infoview/Article_2627.html

62,050

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧