网站安装打包 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一大堆的";

 

打完,收工!

版权声明:本文原创发表于博客园,作者为路过秋天,原文链接:

http://www.cnblogs.com/cyq1162/archive/2010/01/21/1653367.html

相关文章
关于springboot配置文件未加载的问题解决办法
一般情况下springboot的项目的配置文件都是默认加载的properties文件和yaml文件,但是有时候因为一些其他的设置也会导致这些配置文件没有被加载
1681 0
关于springboot配置文件未加载的问题解决办法
|
5月前
|
前端开发 Java 关系型数据库
SpringBoot本地上传文件到resources目录永久保存下载的最佳实践
Java后端项目上传文件是一个很常见的需求,一般正式项目中我们上传文件都是利用第三方阿里云OSS这类的,但是如果只是为了学习之用,那我们可能就会直接上传到电脑上某个本地文件夹
83 1
|
6月前
|
前端开发 Java 应用服务中间件
SpringMVC文件的上传下载&JRebel的使用
SpringMVC文件的上传下载&JRebel的使用
29 0
|
10月前
|
Java Maven
maven打包成功但项目启动时找不到其他包的内容
maven打包成功但项目启动时找不到其他包的内容
145 0
SpringMVC学习(十):文件的上传和下载
在SpringMVC中使用ResponseEntity实现下载文件的功能
|
Java Maven
修改jar包里的配置文件
jar包项目配置文件需要修改,只想修改里面的文件,而不重新使用maven打包。
270 0
|
缓存 NoSQL 前端开发
jeecg中新建接口后报错404的解决方法
jeecg中新建接口后报错404的解决方法
1120 0
jeecg中新建接口后报错404的解决方法
|
Java API Android开发
通过自定义Gradle插件修改编译后的class文件
通过自定义Gradle插件修改编译后的class文件
通过自定义Gradle插件修改编译后的class文件
|
Java
为什么写路径这事有点复杂?动态Web工程内编写路径【JavaWeb】
为什么写路径这事有点复杂?动态Web工程内编写路径【JavaWeb】
70 0
为什么写路径这事有点复杂?动态Web工程内编写路径【JavaWeb】