ASP。NET程序中动态修改web.config中的设置项目 (后台CS代码)

web|程序|动态|后台|项目

朋友们可以自行测试,我这里都没有问题了,鳖了一上午的问题总算解决了

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

时间: 2024-08-03 05:40:19

ASP。NET程序中动态修改web.config中的设置项目 (后台CS代码)的相关文章

来篇文章: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.

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

二、ASP。NET程序中动态修改web.config中的设置项目 (前台页面代码)

web|程序|动态|项目|页面 <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

asp.net2.0中如何修改web.config中指定的键值?

问题描述 <?xmlversion="1.0"?><configuration><configurations><appSettings><!--按钮样式(蓝色:Blue/Style.css,银色:Silver/style.css,浅绿色:Cyan/style.css)--><addkey="ButtonStyleRoot"value="Blue/Style.css"/>&l

在Web.Config文件中使用configSource,避免动态修改web.config导致asp.net重启(另添加一个Config文件用于管理用户数据)

原文:在Web.Config文件中使用configSource,避免动态修改web.config导致asp.net重启(另添加一个Config文件用于管理用户数据) 我们都知道,在asp.net中修改了配置文件web.config后,会导致应用程序重启,所有 会话(session)丢失.然而,应用程序的配置信息放在配置文件里是最佳选择,在后台修改了配置后导致所有会话丢失是非常不爽的事情,这个时候可将配 置文件中经常需要改变的参数配置节 放到外面来,例如appSetting节. 一.原来的web.

运行时修改Web.config中的元素值 zhenyu(原作)

大家都知道Asp.Net提供了一个WebApplication的配置文件web.config,让我们可以很方便地设置Web应用程序,另外还可以在appSettings节中增加一些我们常要使用的单元,如数据库连接串等,利用System.Configuration.ConfigurationSetting类我们可以很容易读取这些单元的值,但是要改变这些值就需要一些技巧了,下面我就来告诉大家如何在运行时利用System.Management中的类去改变这些单元的值. 现假设您有一个WebApplica

一个修改web.config中appSettings配置节的函数

web|函数 这个函数主要使用XmlDocument来解析web.config.并用SelectSingleNode()方法来定位要修改的配置节.要注意的是最后程序要Save(),所以,你的apsnet帐号必须对web.config拥有写权限. -------------------------------------------------------------------------------- /// <summary> /// 修改web.config文件appSettings配置

一个修改web.config中appSettings配置节的函数-Web_Service开发-.NE

这个函数主要使用XmlDocument来解析web.config.并用SelectSingleNode()方法来定位要修改的配置节.要注意的是最后程序要Save(),所以,你的apsnet帐号必须对web.config拥有写权限. -------------------------------------------------------------------------------- /// <summary> /// 修改web.config文件appSettings配置节中的Add里

我如何在网站页面中修改web.config?

问题描述 现在是这样,一个网站,我想在后台管理部分能直接修改web.config里面的参数.这个如何实现啊?另外,我在http://topic.csdn.net/t/20050527/12/4039760.html看到类似的提问,有人给出了例子,可有人说这样不行,因为在项目运行时修改web.config会使Session全部丢失,所以需要用别的方法实现这个功能.那是不是这样呢?望高手指点 解决方案 解决方案二:项目运行的时候没有办法改web.config的解决方案三:你要用页面来改?直接文本打开