a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

C# ConfigHelper 輔助類介紹

2020-01-24 03:29:41
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

//==============================================
//        FileName: ConfigManager
//        Description: 靜態(tài)方法業(yè)務(wù)類,用于對C#、ASP.NET中的WinForm & WebForm 項(xiàng)目程序配置文件
//             app.config和web.config的[appSettings]和[connectionStrings]節(jié)點(diǎn)進(jìn)行新增、修改、刪除和讀取相關(guān)的操作。

//==============================================
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 應(yīng)用程序配置文件管理器
/// </summary>
public class ConfigManager
{
    public ConfigManager()
    {
        //
        // TODO: 在此處添加構(gòu)造函數(shù)邏輯
        //
    }


    /// <summary>
    /// 對[appSettings]節(jié)點(diǎn)依據(jù)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("http://appSettings");   //得到[appSettings]節(jié)點(diǎn)

        ////得到[appSettings]節(jié)點(diǎn)中關(guān)于Key的子節(jié)點(diǎn)
        XmlElement element = (XmlElement)node.SelectSingleNode("http://add[@key='" + key + "']");

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

        return value;
    }

    /// <summary>
    /// 對[connectionStrings]節(jié)點(diǎn)依據(jù)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("http://connectionStrings");   //得到[appSettings]節(jié)點(diǎn)

        ////得到[connectionString]節(jié)點(diǎn)中關(guān)于name的子節(jié)點(diǎn)
        XmlElement element = (XmlElement)node.SelectSingleNode("http://add[@name='" + name + "']");

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

        return connectionString;
    }

    /// <summary>
    /// 更新或新增[appSettings]節(jié)點(diǎn)的子節(jié)點(diǎn)值,存在則更新子節(jié)點(diǎn)Value,不存在則新增子節(jié)點(diǎn),返回成功與否布爾值
    /// </summary>
    /// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
    /// <param name="key">子節(jié)點(diǎn)Key值</param>
    /// <param name="value">子節(jié)點(diǎn)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("http://appSettings");   //得到[appSettings]節(jié)點(diǎn)

        try
        {
            ////得到[appSettings]節(jié)點(diǎn)中關(guān)于Key的子節(jié)點(diǎn)
            XmlElement element = (XmlElement)node.SelectSingleNode("http://add[@key='" + key + "']");

            if (element != null)
            {
                //存在則更新子節(jié)點(diǎn)Value
                element.SetAttribute("value", value);
            }
            else
            {
                //不存在則新增子節(jié)點(diǎn)
                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]節(jié)點(diǎn)的子節(jié)點(diǎn)值,存在則更新子節(jié)點(diǎn),不存在則新增子節(jié)點(diǎn),返回成功與否布爾值
    /// </summary>
    /// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
    /// <param name="name">子節(jié)點(diǎn)name值</param>
    /// <param name="connectionString">子節(jié)點(diǎn)connectionString值</param>
    /// <param name="providerName">子節(jié)點(diǎn)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("http://connectionStrings");   //得到[connectionStrings]節(jié)點(diǎn)

        try
        {
            ////得到[connectionStrings]節(jié)點(diǎn)中關(guān)于Name的子節(jié)點(diǎn)
            XmlElement element = (XmlElement)node.SelectSingleNode("http://add[@name='" + name + "']");

            if (element != null)
            {
                //存在則更新子節(jié)點(diǎn)
                element.SetAttribute("connectionString", connectionString);
                element.SetAttribute("providerName", providerName);
            }
            else
            {
                //不存在則新增子節(jié)點(diǎn)
                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]節(jié)點(diǎn)中包含Key值的子節(jié)點(diǎn),返回成功與否布爾值
    /// </summary>
    /// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
    /// <param name="key">要刪除的子節(jié)點(diǎn)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("http://appSettings");   //得到[appSettings]節(jié)點(diǎn)

        ////得到[appSettings]節(jié)點(diǎn)中關(guān)于Key的子節(jié)點(diǎn)
        XmlElement element = (XmlElement)node.SelectSingleNode("http://add[@key='" + key + "']");

        if (element != null)
        {
            //存在則刪除子節(jié)點(diǎn)
            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]節(jié)點(diǎn)中包含name值的子節(jié)點(diǎn),返回成功與否布爾值
    /// </summary>
    /// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
    /// <param name="name">要刪除的子節(jié)點(diǎn)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("http://connectionStrings");   //得到[connectionStrings]節(jié)點(diǎn)

        ////得到[connectionStrings]節(jié)點(diǎn)中關(guān)于Name的子節(jié)點(diǎn)
        XmlElement element = (XmlElement)node.SelectSingleNode("http://add[@name='" + name + "']");

        if (element != null)
        {
            //存在則刪除子節(jié)點(diǎn)
            node.RemoveChild(element);
        }
        else
        {
            //不存在
        }

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

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

        return isSuccess;
    }

}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 777xacom | 人人草人人 | 免费不卡视频 | 国产精品黄网站在线观看 | 久久久精品网 | 欧美精品h | 日韩在线精品 | 一本一生久久a久久精品综合蜜 | www.久草| 娇喘呻吟趴在雪白肉体耸动图 | 欧美三级电影在线观看 | 另类五月天| 国产小视频在线观看 | 久9久9| 欧美日韩精品免费观看视频 | 亚洲精品白浆高清久久久久久 | 天天干狠狠干 | 国产第六页 | 日韩久久精品 | 99精品热视频 | 黄a免费网络 | 日韩av在线一区二区三区 | 日本精品视频在线播放 | 九九99久久 | 超碰超碰97 | 一级二级在线观看 | 欧美一区二区三区在线看 | 韩日一区二区 | 精品久久久久久亚洲精品 | 欧美日韩在线免费 | 成人午夜天 | 一区二区三区在线 | 亚洲这里只有精品 | 国产精品无码专区在线观看 | 亚洲日本欧美日韩高观看 | 成人久久久 | 久久一区二区视频 | 成人影院一区二区三区 | 天天干人人干 | 欧美成人影院 | 久久久久久久国产精品影院 |