当前位置:  编程技术>.net/c#/asp.net

C# ConfigHelper 辅助类介绍

    来源: 互联网  发布时间:2014-10-18

    本文导语:  代码如下: //==============================================//        FileName: ConfigManager//        Description: 静态方法业务类,用于对C#、ASP.NET中的WinForm & WebForm 项目程序配置文件//             app.config和web.config的[appSettings]和[co...

代码如下:

//==============================================
//        FileName: ConfigManager
//        Description: 静态方法业务类,用于对C#、ASP.NET中的WinForm & WebForm 项目程序配置文件
//             app.config和web.config的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作。

//==============================================
using System;
using System.Data;
using System.Configuration;
using System.Web;

using System.Collections.Generic;
using System.Text;
using System.Xml;

public enum ConfigurationFile
{
    AppConfig=1,
    WebConfig=2
}

///
/// ConfigManager 应用程序配置文件管理器
///
public class ConfigManager
{
    public ConfigManager()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }


    ///
    /// 对[appSettings]节点依据Key值读取到Value值,返回字符串
    ///
    /// 要操作的配置文件名称,枚举常量
    /// 要读取的Key值
    /// 返回Value值的字符串
    public static string ReadValueByKey(ConfigurationFile configurationFile, string key)
    {
        string value = string.Empty;
        string filename = string.Empty;
        if (configurationFile.ToString()==ConfigurationFile.AppConfig.ToString())
        {
            filename = System.Windows.Forms.Application.ExecutablePath + ".config";
        }
        else
        {
            filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config";
        }

        XmlDocument doc = new XmlDocument();
        doc.Load(filename); //加载配置文件

        XmlNode node = doc.SelectSingleNode("//appSettings");   //得到[appSettings]节点

        ////得到[appSettings]节点中关于Key的子节点
        XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");

        if (element != null)
        {
            value = element.GetAttribute("value");
        }

        return value;
    }

    ///
    /// 对[connectionStrings]节点依据name值读取到connectionString值,返回字符串
    ///
    /// 要操作的配置文件名称,枚举常量
    /// 要读取的name值
    /// 返回connectionString值的字符串
    public static string ReadConnectionStringByName(ConfigurationFile configurationFile, string name)
    {
        string connectionString = string.Empty;
        string filename = string.Empty;
        if (configurationFile.ToString() == ConfigurationFile.AppConfig.ToString())
        {
            filename = System.Windows.Forms.Application.ExecutablePath + ".config";
        }
        else
        {
            filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config";
        }

        XmlDocument doc = new XmlDocument();
        doc.Load(filename); //加载配置文件

        XmlNode node = doc.SelectSingleNode("//connectionStrings");   //得到[appSettings]节点

        ////得到[connectionString]节点中关于name的子节点
        XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");

        if (element != null)
        {
            connectionString = element.GetAttribute("connectionString");
        }

        return connectionString;
    }

    ///
    /// 更新或新增[appSettings]节点的子节点值,存在则更新子节点Value,不存在则新增子节点,返回成功与否布尔值
    ///
    /// 要操作的配置文件名称,枚举常量
    /// 子节点Key值
    /// 子节点value值
    /// 返回成功与否布尔值
    public static bool UpdateOrCreateAppSetting(ConfigurationFile configurationFile, string key, string value)
    {
        bool isSuccess = false;
        string filename = string.Empty;
        if (configurationFile.ToString() == ConfigurationFile.AppConfig.ToString())
        {
            filename = System.Windows.Forms.Application.ExecutablePath + ".config";
        }
        else
        {
            filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config";
        }

        XmlDocument doc = new XmlDocument();
        doc.Load(filename); //加载配置文件

        XmlNode node = doc.SelectSingleNode("//appSettings");   //得到[appSettings]节点

        try
        {
            ////得到[appSettings]节点中关于Key的子节点
            XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");

            if (element != null)
            {
                //存在则更新子节点Value
                element.SetAttribute("value", value);
            }
            else
            {
                //不存在则新增子节点
                XmlElement subElement = doc.CreateElement("add");
                subElement.SetAttribute("key", key);
                subElement.SetAttribute("value", value);
                node.AppendChild(subElement);
            }

            //保存至配置文件(方式一)
            using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null))
            {
                xmlwriter.Formatting = Formatting.Indented;
                doc.WriteTo(xmlwriter);
                xmlwriter.Flush();
            }

            isSuccess = true;
        }
        catch (Exception ex)
        {
            isSuccess = false;
            throw ex;
        }

        return isSuccess;
    }

    ///
    /// 更新或新增[connectionStrings]节点的子节点值,存在则更新子节点,不存在则新增子节点,返回成功与否布尔值
    ///
    /// 要操作的配置文件名称,枚举常量
    /// 子节点name值
    /// 子节点connectionString值
    /// 子节点providerName值
    /// 返回成功与否布尔值
    public static bool UpdateOrCreateConnectionString(ConfigurationFile configurationFile, string name, string connectionString, string providerName)
    {
        bool isSuccess = false;
        string filename = string.Empty;
        if (configurationFile.ToString() == ConfigurationFile.AppConfig.ToString())
        {
            filename = System.Windows.Forms.Application.ExecutablePath + ".config";
        }
        else
        {
            filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config";
        }

        XmlDocument doc = new XmlDocument();
        doc.Load(filename); //加载配置文件

        XmlNode node = doc.SelectSingleNode("//connectionStrings");   //得到[connectionStrings]节点

        try
        {
            ////得到[connectionStrings]节点中关于Name的子节点
            XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");

            if (element != null)
            {
                //存在则更新子节点
                element.SetAttribute("connectionString", connectionString);
                element.SetAttribute("providerName", providerName);
            }
            else
            {
                //不存在则新增子节点
                XmlElement subElement = doc.CreateElement("add");
                subElement.SetAttribute("name", name);
                subElement.SetAttribute("connectionString", connectionString);
                subElement.SetAttribute("providerName", providerName);
                node.AppendChild(subElement);
            }

            //保存至配置文件(方式二)
            doc.Save(filename);

            isSuccess = true;
        }
        catch (Exception ex)
        {
            isSuccess = false;
            throw ex;
        }

        return isSuccess;
    }

    ///
    /// 删除[appSettings]节点中包含Key值的子节点,返回成功与否布尔值
    ///
    /// 要操作的配置文件名称,枚举常量
    /// 要删除的子节点Key值
    /// 返回成功与否布尔值
    public static bool DeleteByKey(ConfigurationFile configurationFile, string key)
    {
        bool isSuccess = false;
        string filename = string.Empty;
        if (configurationFile.ToString() == ConfigurationFile.AppConfig.ToString())
        {
            filename = System.Windows.Forms.Application.ExecutablePath + ".config";
        }
        else
        {
            filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config";
        }

        XmlDocument doc = new XmlDocument();
        doc.Load(filename); //加载配置文件

        XmlNode node = doc.SelectSingleNode("//appSettings");   //得到[appSettings]节点

        ////得到[appSettings]节点中关于Key的子节点
        XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");

        if (element != null)
        {
            //存在则删除子节点
            element.ParentNode.RemoveChild(element);
        }
        else
        {
            //不存在
        }

        try
        {
            //保存至配置文件(方式一)
            using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null))
            {
                xmlwriter.Formatting = Formatting.Indented;
                doc.WriteTo(xmlwriter);
                xmlwriter.Flush();
            }

            isSuccess = true;
        }
        catch (Exception ex)
        {
            isSuccess = false;
        }

        return isSuccess;
    }

    ///
    /// 删除[connectionStrings]节点中包含name值的子节点,返回成功与否布尔值
    ///
    /// 要操作的配置文件名称,枚举常量
    /// 要删除的子节点name值
    /// 返回成功与否布尔值
    public static bool DeleteByName(ConfigurationFile configurationFile, string name)
    {
        bool isSuccess = false;
        string filename = string.Empty;
        if (configurationFile.ToString() == ConfigurationFile.AppConfig.ToString())
        {
            filename = System.Windows.Forms.Application.ExecutablePath + ".config";
        }
        else
        {
            filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config";
        }

        XmlDocument doc = new XmlDocument();
        doc.Load(filename); //加载配置文件

        XmlNode node = doc.SelectSingleNode("//connectionStrings");   //得到[connectionStrings]节点

        ////得到[connectionStrings]节点中关于Name的子节点
        XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");

        if (element != null)
        {
            //存在则删除子节点
            node.RemoveChild(element);
        }
        else
        {
            //不存在
        }

        try
        {
            //保存至配置文件(方式二)
            doc.Save(filename);

            isSuccess = true;
        }
        catch (Exception ex)
        {
            isSuccess = false;
        }

        return isSuccess;
    }

}


    
 
 

您可能感兴趣的文章:

 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3