网站安装打包 webconfig修改[三]

在net中,在System.Configuration.ConfigurationManager中,提供了几个静态方法,用来修改配置文件。

如:System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMachineConfiguration();

获得应用程序下的配置文件,之后再用config进行操作。

如果是在web中,那就是操作webconfig了!不过现在在winform中,就成了操作app.config了。

 


 

于是,我选择了还是以操作xml的方式来修改webconfig。

这里写了几个类,主要也是模仿config的操作方式。代码如下:

 

ExpandedBlockStart.gif 代码
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Xml;
namespace  IISHelper
{
    
public   class  WebConfigHelper : IDisposable
    {
        
private   bool  loadIsOK;
        
///   <summary>
        
///  加载是否成功
        
///   </summary>
         public   bool  LoadIsOK
        {
            
get  {  return  loadIsOK; }
            
set  { loadIsOK  =  value; }
        }

        
private  XmlDocument xDox  =   new  XmlDocument();
        
private   string  configPath  =   string .Empty;
        
public  WebConfigHelper( string  webconfigPath)
        {
            
try
            {
                xDox.Load(webconfigPath);
                configPath 
=  webconfigPath;
                loadIsOK 
=   true ;
            }
            
catch  { loadIsOK  =   false ; }
            
        }
        
public  WebConfigAppSetting AppSetting
        {
            
get
            {
                XmlNode xNode
= xDox.SelectSingleNode( " //configuration/appSettings " );
                
if (xNode == null )
                {
                    
return   null ;
                }
                
return   new  WebConfigAppSetting( ref  xNode);
            }
        }
        
public  WebConfigConnectionStrings ConnectionStrings
        {
            
get
            {
                XmlNode xNode 
=  xDox.SelectSingleNode( " //configuration/connectionStrings " );
                
if  (xNode  ==   null )
                {
                    
return   null ;
                }
                
return   new  WebConfigConnectionStrings( ref  xNode);
            }
        }
        
public   bool  Save()
        {
            
try
            {
                xDox.Save(configPath);
                
return   true ;
            }
            
catch  { }
            
return   false ;
        }
        
#region  IDisposable 成员
        
public   void  Dispose()
        {
            xDox 
=   null ;
        }
        
#endregion
    }
    
public   abstract   class  WebConfigBase
    {
        
protected  XmlNode node;
        
public    void  Add( string  key,  string  value){}
        
public   abstract   void  Set( string  key,  string  value);
        
public   abstract   string  Get( string  key);
        
public    void  Remove( string  key,  string  value){}
    }
    
public   class  WebConfigAppSetting : WebConfigBase
    {
        
internal   WebConfigAppSetting( ref  XmlNode xNode)
        {
            node 
=  xNode;
        }
        
public   override   void  Set( string  key,  string  value)
        {
            
foreach  (XmlNode addNode  in  node.ChildNodes)
            {
                
if  (addNode.Attributes  !=   null   &&  addNode.Attributes[ " key " ].Value  ==  key)
                {
                    addNode.Attributes[
" value " ].Value  =  value;
                    
break ;
                }
            }
        }
        
public   override   string  Get( string  key)
        {
            
foreach  (XmlNode addNode  in  node.ChildNodes)
            {
                
if  (addNode.Attributes  !=   null   &&  addNode.Attributes[ " key " ].Value  ==  key)
                {
                  
return   addNode.Attributes[ " value " ].Value;
                }
            }
            
return   "" ;
        }
    }
    
public   class  WebConfigConnectionStrings : WebConfigBase
    {
        
internal   WebConfigConnectionStrings( ref  XmlNode xNode)
        {
            node 
=  xNode;
        }
        
public   override   void  Set( string  key,  string  value)
        {
            
foreach  (XmlNode addNode  in  node.ChildNodes)
            {
                
if  (addNode.Attributes  !=   null   &&  addNode.Attributes[ " name " ].Value  ==  key)
                {
                    addNode.Attributes[
" connectionString " ].Value  =  value;
                    
break ;
                }
            }
        }
        
public   override   string  Get( string  key)
        {
            
foreach  (XmlNode addNode  in  node.ChildNodes)
            {
                
if  (addNode.Attributes  !=   null   &&  addNode.Attributes[ " name " ].Value  ==  key)
                {
                    
return  addNode.Attributes[ " connectionString " ].Value;
                }
            }
            
return   "" ;
        }
    }
}

 

 下面看一下界面的操作方法:

 

ExpandedBlockStart.gif 界面操作方式
 WebConfigHelper allConfig  =   new  WebConfigHelper(allConfigPath);
            
if  (allConfig.LoadIsOK )
            {
                WebConfigAppSetting app 
=  allConfig.AppSetting;
                
if  (app  !=   null )
                {
                  
  app.Set( " MosFTPUserName " , txtMosFtpUserName.Text);
                    app.Set(
" MosFTPPassword " , txtMosFtpPassword.Text);
                    app.Set(
" ProvideFor " , cbbProvideFor.Text);
                 
}
                WebConfigConnectionStrings connString 
=  allConfig.ConnectionStrings;
                
if  (connString  !=   null )
                {
                    connString.Set(
" Conn " , txtConn.Text);
                }
                allConfig.Save();
                allConfig.Dispose();

               
 MessageBox.Show( " 配置文件修改成功! " );
            }

 

 这里提示一下,web.config中,不要带名称空间,就是xmlns="xxxx一大堆的";

 

打完,收工!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值