亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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#窗體通訊錄系統(tǒng)的示例代碼

    C#窗體通訊錄系統(tǒng)的示例代碼

    本文主要介紹了C#窗體通訊錄系統(tǒng)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • 最新評(píng)論