Web.config中的设置能否在程序运行中动态改变某些设置的值?

chequan 2004-12-21 06:43:04
比如
<authentication mode="Forms">
<forms name="myAuth" protection="All" timeout="60" loginUrl="login.aspx" />
</authentication>
但是,我只是在运行中改变某些值,并不真实改变Web.config文件的内容。请问如何做?谢谢!
...全文
175 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
gxsun 2005-05-13
  • 打赏
  • 举报
回复
mark
jackcjq 2005-01-20
  • 打赏
  • 举报
回复
程序一运行,所有配置信息就被读取到了,读取到后也是知读的,没法再改变设置吧,只有改变config设置,再重新读取这个值。我想是这样的。
chequan 2004-12-22
  • 打赏
  • 举报
回复
唉,大家都没看懂我的意思吗?
microhf 2004-12-21
  • 打赏
  • 举报
回复
请用代码测试你地想法。
Shanks_YH 2004-12-21
  • 打赏
  • 举报
回复
有个很笨的方法,IO文件操作,我曾经试过,还行
xxwnet 2004-12-21
  • 打赏
  • 举报
回复
学习
chequan 2004-12-21
  • 打赏
  • 举报
回复
我的意思是比如
<forms name="myAuth" protection="All" timeout="60" loginUrl="login.aspx" />
这样的设置,在ASP.NET中对应FormsAuthentication中的某些属性,但是这些属性是只读的。我并不想真实改变Web.config文件的内容,而只是希望通过某种途径,达到修改了FormsAuthentication中的某些只读属性的值的目的(这些属性的内容本来是直接读取的Web.config中的<forms>配置节的)
softchao 2004-12-21
  • 打赏
  • 举报
回复
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript (ECMAScript)">
<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:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 164px; POSITION: absolute; TOP: 190px" runat="server" Width="204px" Height="25px"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 386px; POSITION: absolute; TOP: 188px" runat="server" Width="78px" Height="25px" Text="Button"></asp:Button>
<asp:DropDownList id="DropDownList1" style="Z-INDEX: 103; LEFT: 29px; POSITION: absolute; TOP: 192px" runat="server" Width="130px" Height="22px"></asp:DropDownList>
</FONT>
</form>
</body>
</HTML>
softchao 2004-12-21
  • 打赏
  • 举报
回复
ASP.NET程序中动态修改web.config中的设置项目(后台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 ;


namespace WebApplication1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Button Button1;

public WebForm1()
{
Page.Init += new System.EventHandler(Page_Init);
}

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
//打开某文件(假设WEB。CONFIG在根目录中)
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.ToLower()=="appsettings")
{
XmlNodeList _node=element.ChildNodes;
if ( _node.Count >0 )
{
DropDownList1.Items.Clear();
foreach(XmlElement el in _node)
{
DropDownList1.Items.Add(el.Attributes["key"].InnerXml);
}
}
}
}
}
}

private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}

#region Web Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </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)
{
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.ToLower()=="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["value"].Value=this.TextBox1.Text;
}
}
}
}
}
xmldoc.Save(filename);
}
}
}


chequan 2004-12-21
  • 打赏
  • 举报
回复
比如,改变<forms>节的name、timeout等属性的值,但是只改变运行时变量的值,不真实改变Web.config文件。

62,046

社区成员

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

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

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

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