在做网站时,想动态修改Web.Config,怎么改啊?高手给一段代码看看!刚学

sails1978 2004-07-20 01:34:19
如题!
...全文
144 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sails1978 2004-07-20
  • 打赏
  • 举报
回复
第一次用,没想到回得这么快!谢谢!
dotnet3000 2004-07-20
  • 打赏
  • 举报
回复
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;

}
}
}
acewang 2004-07-20
  • 打赏
  • 举报
回复
see:
Runtime Web.config / App.config Editing
http://www.eggheadcafe.com/articles/20030907.asp
大雨仔 2004-07-20
  • 打赏
  • 举报
回复
现假设您有一个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是否已被成功修改.
不知道对不对!
DaragonShop系统源码 DaragonShop系统(vs2008+SQL2005)是在课余间随手制作的,主要是对一些基础的巩固和三层架构体系的运用,包含反射的应用等。 主要涉及:主流三层架构+工厂反射模式+ sql自带函数分页(曾经写过一个网站分别用了四种不同的分页模块,但因网络硬盘文件丢失,笔记本也在搬行李失踪不能共享,遗憾) 有兴趣的朋友或者一些正在学习的C#的同学可以看一看。 在DaragonShopData文件夹中是系统的数据库(sqlserver 2005版) ,加载即可。然后Web.Config连接字符串即可,当然如果你是刚刚学习的,不知道连接字符串怎么,那么好吧,我就给你说一个非常非常简单的方法,但在说这个之前我要说一下,个人是非常讨厌用数据库连接控件的,但是在连接字符串的候挺方便的,直接新建一个页面拖一个数据连接控件,选择要连的数据库一直点下一步,完成之后在web.config节点下就会出现了,把名为DragonShopConnection 的值替换掉即可。 后台因为没有模板,也没多少间,在候就直接就用GridView实现了大概删除的功能,有点简陋凑合着看吧。(如果有模板的朋友可以给我传一套,我可以更新 QQ:715501675) 今天上QQ发现自己不知道什么候居然建过一个群,如果有兴趣的朋友可以进来提问:80012346。 这个系统我会在以后不断更新,也欢迎各位朋友多给点意见,当然高手要是要是看不起咱这小程序也不勉强(嘎嘎,鄙人只希望能够相互学习,这样大家才能一块进步.....). 简单介绍下我用过的四种分页模式,因为刚网站候这块是比较烦人的。这样有兴趣的朋友可以有个大致的方向 1.当然是这个系统里面的,用存储过程,用SQL里面的一个排序函数,建立虚拟表,调用需要的表段。 2.在写sql语句的候采用select top n where .... not in (select top n*page where ....) 的方法直接到数据库里找 3.在.net里面自带的有个pagedatasource的东东,用这个分页,然后把它当数据源传给控件,怎么弄自己上网找。 4.这个是很蠢得了,直接用控件分页,不说太多,最简单的。

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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