国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C# ConfigHelper 輔助類介紹

2020-01-24 03:29:41
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

//==============================================
//        FileName: ConfigManager
//        Description: 靜態(tài)方法業(yè)務(wù)類,用于對(duì)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>
    /// 對(duì)[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>
    /// 對(duì)[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ǎng)h除的子節(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ǎng)h除的子節(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ā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 葵青区| 田东县| 东乡族自治县| 九龙坡区| 剑河县| 临桂县| 崇阳县| 布拖县| 临汾市| 历史| 肥城市| 沙雅县| 抚州市| 二连浩特市| 砚山县| 漳平市| 清河县| 阳原县| 沈丘县| 尉犁县| 肇庆市| 莆田市| 肥东县| 肇州县| 象山县| 天祝| 庆阳市| 彭泽县| 江门市| 永德县| 龙川县| 察哈| 南江县| 古蔺县| 凤凰县| 长白| 宁国市| 浮梁县| 邳州市| 社旗县| 屏南县|