zoukankan      html  css  js  c++  java
  • 网站安装打包 webconfig修改[三]

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

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

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

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


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

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

    代码
    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 "";
            }
        }
    }

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

    界面操作方式
     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一大堆的";

    打完,收工!

    版权声明:本文原创发表于 博客园,作者为 路过秋天 本文欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。
    个人微信公众号
    创业QQ群:617713515
    Donation(扫码支持作者):支付宝:
    Donation(扫码支持作者):微信:
  • 相关阅读:
    换行的展示
    jsp页面的导出功能
    怎么设置回车键为提交功能?
    HBuilder使用心得
    js和jQuery
    前端常用技术总结--java程序员
    对压缩文件加密
    删除表中一个字段的SQL语句
    用NPOI操作EXCEL-锁定列CreateFreezePane()
    MVC 点击下载文档
  • 原文地址:https://www.cnblogs.com/cyq1162/p/1653367.html
Copyright © 2011-2022 走看看