欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# ConfigHelper 辅助类介绍

程序员文章站 2024-02-11 19:09:40
复制代码 代码如下: //==============================================//    ...

复制代码 代码如下:

//==============================================
//        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
}

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


    /// <summary>
    /// 对[appsettings]节点依据key值读取到value值,返回字符串
    /// </summary>
    /// <param name="configurationfile">要操作的配置文件名称,枚举常量</param>
    /// <param name="key">要读取的key值</param>
    /// <returns>返回value值的字符串</returns>
    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;
    }

    /// <summary>
    /// 对[connectionstrings]节点依据name值读取到connectionstring值,返回字符串
    /// </summary>
    /// <param name="configurationfile">要操作的配置文件名称,枚举常量</param>
    /// <param name="name">要读取的name值</param>
    /// <returns>返回connectionstring值的字符串</returns>
    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;
    }

    /// <summary>
    /// 更新或新增[appsettings]节点的子节点值,存在则更新子节点value,不存在则新增子节点,返回成功与否布尔值
    /// </summary>
    /// <param name="configurationfile">要操作的配置文件名称,枚举常量</param>
    /// <param name="key">子节点key值</param>
    /// <param name="value">子节点value值</param>
    /// <returns>返回成功与否布尔值</returns>
    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;
    }

    /// <summary>
    /// 更新或新增[connectionstrings]节点的子节点值,存在则更新子节点,不存在则新增子节点,返回成功与否布尔值
    /// </summary>
    /// <param name="configurationfile">要操作的配置文件名称,枚举常量</param>
    /// <param name="name">子节点name值</param>
    /// <param name="connectionstring">子节点connectionstring值</param>
    /// <param name="providername">子节点providername值</param>
    /// <returns>返回成功与否布尔值</returns>
    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;
    }

    /// <summary>
    /// 删除[appsettings]节点中包含key值的子节点,返回成功与否布尔值
    /// </summary>
    /// <param name="configurationfile">要操作的配置文件名称,枚举常量</param>
    /// <param name="key">要删除的子节点key值</param>
    /// <returns>返回成功与否布尔值</returns>
    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;
    }

    /// <summary>
    /// 删除[connectionstrings]节点中包含name值的子节点,返回成功与否布尔值
    /// </summary>
    /// <param name="configurationfile">要操作的配置文件名称,枚举常量</param>
    /// <param name="name">要删除的子节点name值</param>
    /// <returns>返回成功与否布尔值</returns>
    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;
    }

}