小山

付出最大努力,追求最高成就,享受最佳生活,收获无悔人生

博客园 首页 新随笔 联系 订阅 管理
  200 Posts :: 1 Stories :: 1253 Comments :: 76 Trackbacks

1.建立一个class,ReadWriteConfig.cs
using System;
using System.Configuration;
using System.Reflection;
using System.Web;
using System.Xml;
public enum ConfigFileType
{
    WebConfig,
    AppConfig
}


namespace WebApplication1
{
    
/// <summary>
    
/// Summary description for ReadWriteConfig.
    
/// </summary>

    public class ReadWriteConfig
    
{        
        
public string docName = String.Empty; 
        
private XmlNode node=null;    
        
private int _configType;  
        
public int ConfigType
        
{
            
getreturn _configType; }
            
set{ _configType=value; }
        }
  

        
SetValue

        
saveConfigDoc

        
removeElement

        
modifyElement

        
loadConfigDoc
    }

}
2.建立测试页面
2.1 html
<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">
            
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 296px; POSITION: absolute; TOP: 120px" runat="server"
                Text
="删除"></asp:Button>
            
<asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 232px; POSITION: absolute; TOP: 120px" runat="server"
                Text
="新增"></asp:Button>
            
<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 104px; POSITION: absolute; TOP: 32px" runat="server">key</asp:Label>
            
<asp:TextBox id="TextBox1" style="Z-INDEX: 104; LEFT: 168px; POSITION: absolute; TOP: 32px" runat="server"></asp:TextBox>
            
<asp:Label id="Label2" style="Z-INDEX: 105; LEFT: 104px; POSITION: absolute; TOP: 96px" runat="server">value</asp:Label>
            
<asp:TextBox id="TextBox2" style="Z-INDEX: 106; LEFT: 168px; POSITION: absolute; TOP: 80px" runat="server"></asp:TextBox>
            
<asp:Button id="Button3" style="Z-INDEX: 107; LEFT: 360px; POSITION: absolute; TOP: 120px" runat="server"
                Text
="修改"></asp:Button>
        
</form>
    
</body>
</HTML>
2.2 cs代码
public class WebForm1 : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Button Button1;
        
protected System.Web.UI.WebControls.Label Label1;
        
protected System.Web.UI.WebControls.TextBox TextBox1;
        
protected System.Web.UI.WebControls.Label Label2;
        
protected System.Web.UI.WebControls.TextBox TextBox2;
        
protected System.Web.UI.WebControls.Button Button3;
        
protected System.Web.UI.WebControls.Button Button2;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
        }


        
Web Form Designer generated code

        
private void Button2_Click(object sender, System.EventArgs e)
        
{
            
//新增
            ReadWriteConfig config = new ReadWriteConfig();   
            config.ConfigType 
= (int)ConfigFileType.WebConfig; 
            config.SetValue(
this.TextBox1.Text,this.TextBox2.Text);
        }


        
private void Button1_Click(object sender, System.EventArgs e)
        
{
            
//删除
            ReadWriteConfig config = new ReadWriteConfig();   
            config.ConfigType 
= (int)ConfigFileType.WebConfig; 
            config.removeElement(
this.TextBox1.Text);
        }


        
private void Button3_Click(object sender, System.EventArgs e)
        
{
            
//修改
            ReadWriteConfig config = new ReadWriteConfig();   
            config.ConfigType 
= (int)ConfigFileType.WebConfig; 
            config.SetValue(
this.TextBox1.Text,this.TextBox2.Text);
        }

    }
3.测试代码下载/Files/singlepine/readwritconfig.rar
0
0
(请您对文章做出评价)
« 上一篇:Ajax实现无刷新三联动下拉框
» 下一篇:SEO|搜索引擎优化(如何让你的网站排名靠前)
posted on 2005-12-09 12:41 小山 阅读(4287) 评论(11)  编辑 收藏 网摘 所属分类: ASP.NET

Feedback

太棒了,真是受益匪浅!
  回复  引用    

#2楼 2006-03-13 11:08 zkxp      
好啊,我一直就想怎么在线修改之呢。
  回复  引用  查看    

#3楼 2006-04-14 14:59 uyou[未注册用户]
你能帮我改写为vb的吗?谢谢
uyou1@163.com

  回复  引用    

http://singlepine.cnblogs.com/articles/293683.html
这里写得很不错,呵呵


  回复  引用    

#5楼 2006-05-14 18:34 chyna[未注册用户]
程序运行后怎么不能修改那个 web。config 文件?
  回复  引用    

#6楼 2006-06-21 18:09 PJH[未注册用户]
那里面的项为什么要先删除再添加了,不能直接修改吗?
  回复  引用    

不错啊!◎非常感谢!
  回复  引用    

如果这样动态修改web.config文件会不会导致session失效?
  回复  引用    


每次去上你的csdn,都会学到很多东西,谢谢了 。

  回复  引用    

#10楼 2009-04-21 10:31 shopia[未注册用户]
使用如上代码后,不能对Web.config进行修改,错误为:对路径"G:\WebSite\webBLEMS\WEBUI\web.config“的访问被拒绝。这个Web.config并非为只读文件。请问问题出现在哪里?请指教!
  回复  引用    

#11楼 2009-12-28 20:28 jes      
modifyElement方法和removeElement方法一模一样啊

是不是写错了?

  回复  引用  查看