对web.config进行新增修改删除读取操作


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

        
#region SetValue
        
public bool SetValue(string key, string value)
        
{
            XmlDocument cfgDoc 
= new XmlDocument();
            loadConfigDoc(cfgDoc);   
            
// retrieve the appSettings node   
            node =  cfgDoc.SelectSingleNode("//appSettings");      
            
if( node == null )
            
{
                
throw new InvalidOperationException( "appSettings section not found");
            }
     
            
try  
            
{   
                
// XPath select setting "add" element that contains this key       
                XmlElement addElem= (XmlElement)node.SelectSingleNode("//add[@key='" +key +"']") ;
                
if(addElem!=null)
                
{
                    addElem.SetAttribute(
"value",value);
                }
    
                
// not found, so we need to add the element, key and value   
                else
                
{
                    XmlElement entry 
= cfgDoc.CreateElement("add");
                    entry.SetAttribute(
"key",key);    
                    entry.SetAttribute(
"value",value);    
                    node.AppendChild(entry);
                }
   
                
//save it   
                saveConfigDoc(cfgDoc,docName);   
                
return true;  
            }
  
            
catch
            
{
                
return false;
            }
 
        }
  
        
        
#endregion


        
#region saveConfigDoc
        
private void saveConfigDoc(XmlDocument cfgDoc,string cfgDocPath)
        
{
            
try
            
{
                XmlTextWriter writer 
= new XmlTextWriter( cfgDocPath , null ); 
                writer.Formatting 
= Formatting.Indented;    
                cfgDoc.WriteTo( writer );    
                writer.Flush(); 
                writer.Close();  
                
return;
            }
  
            
catch
            
{
                
throw;
            }

        }
 
        
        
#endregion


        
#region removeElement
        
public bool removeElement (string elementKey) 
        

            
try  
            
{   
                XmlDocument cfgDoc 
= new XmlDocument(); 
                loadConfigDoc(cfgDoc);  
                
// retrieve the appSettings node  
                node =  cfgDoc.SelectSingleNode("//appSettings");   
                
if( node == null )
                
{
                    
throw new InvalidOperationException( "appSettings section not found");
                }
     
                
// XPath select setting "add" element that contains this key to remove      
                node.RemoveChild( node.SelectSingleNode("//add[@key='" +elementKey +"']") ); 
                saveConfigDoc(cfgDoc,docName);  
                
return true
            }
 
            
catch
            
{
                
return false;
            }
 
        }
        
        
#endregion


        
#region modifyElement
        
public bool modifyElement (string elementKey) 
        

            
try  
            
{   
                XmlDocument cfgDoc 
= new XmlDocument(); 
                loadConfigDoc(cfgDoc);  
                
// retrieve the appSettings node  
                node =  cfgDoc.SelectSingleNode("//appSettings");   
                
if( node == null )
                
{
                    
throw new InvalidOperationException( "appSettings section not found");
                }
     
                
// XPath select setting "add" element that contains this key to remove      
                node.RemoveChild(node.SelectSingleNode("//add[@key='" +elementKey +"']")); 
                saveConfigDoc(cfgDoc,docName);  
                
return true
            }
 
            
catch
            
{
                
return false;
            }
 
        }
        
        
#endregion


        
#region loadConfigDoc
        
private XmlDocument loadConfigDoc( XmlDocument cfgDoc ) 
        
{   
            
// load the config file   
            if(  Convert.ToInt32(ConfigType)==Convert.ToInt32(ConfigFileType.AppConfig))
            
{
                docName
= ((Assembly.GetEntryAssembly()).GetName()).Name;   
                docName 
+=   ".exe.config";
            }
 
            
else
            
{
                docName
=HttpContext.Current.Server.MapPath("web.config");
            }
  
            cfgDoc.Load( docName );  
            
return cfgDoc;
        }
 
        
#endregion

    }

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

    }

 

http://singlepine.cnblogs.com/articles/293683.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值