最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# ConfigHelper 輔助類介紹

 更新時(shí)間:2013年04月03日 10:14:37   作者:  
ConfigHelper(包含AppConfig和WebConfig), app.config和web.config的[appSettings]和[connectionStrings]節(jié)點(diǎn)進(jìn)行新增、修改、刪除和讀取相關(guān)的操作。

復(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;
    }

}

相關(guān)文章

  • c#連接excel示例分享

    c#連接excel示例分享

    這篇文章主要介紹了c#連接excel示例,需要注意excel版本的引擎問題,需要的朋友可以參考下
    2014-02-02
  • C#實(shí)現(xiàn)語音播報(bào)功能

    C#實(shí)現(xiàn)語音播報(bào)功能

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)語音播報(bào)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 深入解析C#編程中struct所定義的結(jié)構(gòu)

    深入解析C#編程中struct所定義的結(jié)構(gòu)

    這篇文章主要介紹了C#編程中struct所定義的結(jié)構(gòu),與C++一樣,C#語言同時(shí)擁有類和結(jié)構(gòu),需要的朋友可以參考下
    2016-01-01
  • C#?從?UTF-8?流中讀取字符串的正確方法及代碼詳解

    C#?從?UTF-8?流中讀取字符串的正確方法及代碼詳解

    在本篇文章里小編給大家整理的是一篇關(guān)于C#?從?UTF-8?流中讀取字符串的正確方法的知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-11-11
  • C#實(shí)現(xiàn)文件上傳下載Excel文檔示例代碼

    C#實(shí)現(xiàn)文件上傳下載Excel文檔示例代碼

    這篇文章主要介紹了C#實(shí)現(xiàn)文件上傳下載Excel文檔示例代碼,需要的朋友可以參考下
    2017-08-08
  • C#利用win32 Api 修改本地系統(tǒng)時(shí)間、獲取硬盤序列號(hào)

    C#利用win32 Api 修改本地系統(tǒng)時(shí)間、獲取硬盤序列號(hào)

    這篇文章主要介紹了C#利用win32 Api 修改本地系統(tǒng)時(shí)間、獲取硬盤序列號(hào)的方法及代碼分享,需要的朋友可以參考下
    2015-03-03
  • C#中的Task.Delay()和Thread.Sleep()區(qū)別(代碼案例)

    C#中的Task.Delay()和Thread.Sleep()區(qū)別(代碼案例)

    Task.Delay(),async/await和CancellationTokenSource組合起來使用可以實(shí)現(xiàn)可控制的異步延遲。本文通過多種代碼案例給大家分析C#中的Task.Delay()和Thread.Sleep()知識(shí),感興趣的朋友一起看看吧
    2021-06-06
  • C#使用SevenZipSharp實(shí)現(xiàn)壓縮文件和目錄

    C#使用SevenZipSharp實(shí)現(xiàn)壓縮文件和目錄

    SevenZipSharp壓縮/解壓(.7z?.zip)”是指使用SevenZipSharp庫進(jìn)行7z和zip格式的文件壓縮與解壓縮操作,SevenZipSharp是C#語言封裝的7-Zip?API,它使得在.NET環(huán)境中調(diào)用7-Zip的功能變得簡單易行,本文給大家介紹了C#使用SevenZipSharp實(shí)現(xiàn)壓縮文件和目錄
    2025-01-01
  • c#菜單動(dòng)態(tài)合并的實(shí)現(xiàn)方法

    c#菜單動(dòng)態(tài)合并的實(shí)現(xiàn)方法

    這篇文章主要介紹了c#菜單動(dòng)態(tài)合并的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • C#中屬性(Attribute)的用法

    C#中屬性(Attribute)的用法

    這篇文章介紹了C#中屬性(Attribute)的用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05

最新評(píng)論

英吉沙县| 西安市| 会同县| 新郑市| 甘南县| 浑源县| 绥芬河市| 田阳县| 连南| 和顺县| 铜山县| 四子王旗| 伊宁县| 灵丘县| 南和县| 嘉荫县| 文化| 浙江省| 海晏县| 贺兰县| 吉隆县| 正安县| 长宁县| 孝义市| 邵东县| 太仆寺旗| 合川市| 亳州市| 香格里拉县| 眉山市| 宣化县| 遵义县| 司法| 连州市| 屏边| 金川县| 清苑县| 沂水县| 合水县| 手游| 武隆县|