小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C#操作配置文件中appSetting,connectionStrings節(jié)點(diǎn)

 昵稱1519261 2010-09-06
下面的代碼是我看完周金橋老師的ASP.NET夜話,以及上網(wǎng)查了一些東西后,自己寫的關(guān)于對(duì)配置文件中 appSettings,connectionStrings節(jié)點(diǎn)的操作。關(guān)于配置文件的知識(shí),我建議大家到周金橋老師的博客,去學(xué)習(xí)一下,我覺(jué)得他講的 較詳細(xì)些,僅僅是一個(gè)建議。如果你對(duì)我寫的代碼有什么想法或者疑問(wèn),請(qǐng)給我發(fā)郵件或加我QQ(郵件:baiyu311@yahoo.cn QQ:602316022)
說(shuō)明一下,我現(xiàn)在還是個(gè)學(xué)生,有些技術(shù)上的問(wèn)題未必會(huì)懂,但我堅(jiān)信通過(guò)我們的努力是可以客服它的。還有我最近在學(xué)習(xí)Linux,很想做一個(gè)Linux人,開(kāi)源嘛,如果你有興趣也可以聯(lián)系我。期待每位上進(jìn)的朋友,嘿嘿。。。。下面是我的代碼:
using System;
using System.Configuration;
using System.Web;
using System.Web.Configuration;

namespace myConfiguration
{
    #region 配置信息的操作類
    /// <summary>
    /// 配置信息的操作
    /// </summary>
    public class ConfigurationOperator:IDisposable
    {
        #region 變量的聲明
        /// <summary>
        /// Configuration Object
        /// </summary>
        private Configuration config;
        #endregion

        #region 構(gòu)造函數(shù),有參數(shù)(當(dāng)前應(yīng)用程序的虛擬路徑)
        /// <summary>
        /// 構(gòu)造函數(shù),有參數(shù)(當(dāng)前應(yīng)用程序的虛擬路徑)
        /// </summary>
        public ConfigurationOperator()
            : this(HttpContext.Current.Request.ApplicationPath)
        {

        }
        #endregion

        #region 構(gòu)造函數(shù),有參數(shù)(其他應(yīng)用程序的虛擬路徑)
        /// <summary>
        /// 構(gòu)造函數(shù),有參數(shù)(其他應(yīng)用程序的虛擬路徑)
        /// </summary>
        /// <param name="path">其他應(yīng)用程序的虛擬路徑</param>
        public ConfigurationOperator(string path)
        {
            config = WebConfigurationManager.OpenWebConfiguration(path);
        }
        #endregion

        #region 獲取當(dāng)前或其他應(yīng)用程序配置文件中appSettings的所有keyName方法
        /// <summary>
        /// 定義獲取當(dāng)前或其他應(yīng)用程序appSettings的所有keyName方法
        /// </summary>
        /// <returns>返回appSettings的所有keyName</returns>
        public string[] ActiveALLAppSettingsSection()
        {
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
            string[] appKeys = appSettings.Settings.AllKeys;
            return appKeys;
        }
        #endregion

        #region 設(shè)置當(dāng)前或者其他應(yīng)用程序配置文件中的appSettings節(jié)點(diǎn)
        /// <summary>
        /// 定義設(shè)置當(dāng)前或者其他應(yīng)用程序配置文件中的appSettings節(jié)點(diǎn)
        /// </summary>
        /// <param name="key">keyName</param>
        /// <param name="value">keyValue</param>
        public void SetAppSettingsSection(string key,string value)
        {
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
            if (appSettings.Settings[key]!=null)
            {
                appSettings.Settings[key].Value = value;
                this.Save();
            }
            else
            {
                appSettings.Settings.Add(key, value);
                this.Save();
            }
        }
        #endregion

        #region 刪除當(dāng)前或者其他應(yīng)用程序配置文件中的appSettings節(jié)點(diǎn)
        /// <summary>
        /// 定義刪除當(dāng)前或者其他應(yīng)用程序配置文件中的appSettings節(jié)點(diǎn)
        /// </summary>
        /// <param name="key">keyName</param>
        /// <returns>刪除成功返回true,刪除失敗返回false</returns>
        public bool RemoveAppSettingsSection(string key)
        {
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
            if (appSettings.Settings[key] != null)
            {
                appSettings.Settings.Remove(key);
                this.Save();
                return true;
            }
            else
            {
                return false;
            }

        }
        #endregion

        #region 獲取當(dāng)前或其他應(yīng)用程序配置文件中connectionStrings節(jié)點(diǎn)的所有ConnectionString
        /// <summary>
        /// 定義獲取當(dāng)前或其他應(yīng)用程序配置文件中connectionStrings節(jié)點(diǎn)的所有ConnectionString
        /// </summary>
        /// <returns>返回connectionStrings節(jié)點(diǎn)的所有ConnectionString</returns>
        public string[] ALLConnectionStrings()
        {
            ConnectionStringsSection conSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
            ConnectionStringSettingsCollection conCollection = conSection.ConnectionStrings;
            string[] conStrings = new string[conSection.ConnectionStrings.Count];
            int i = 0;
            foreach (ConnectionStringSettings conSetting in conCollection)
            {
                conStrings[i++] = conSetting.ConnectionString;
            }
            return conStrings;
        }
        #endregion

        #region 設(shè)置當(dāng)前或其他應(yīng)用程序配置文件中ConnectionString節(jié)點(diǎn)
        /// <summary>
        /// 定義設(shè)置當(dāng)前或其他應(yīng)用程序配置文件中ConnectionString節(jié)點(diǎn)
        /// </summary>
        /// <param name="name">connectionStrings Name</param>
        /// <param name="ConnectionString">connectionStrings ConnectionString</param>
        /// <param name="providerName">connectionStrings ProviderName</param>
        public void SetConnectionStringsSection(string name, string ConnectionString, string providerName)
        {
            ConnectionStringsSection conSection=(ConnectionStringsSection)config.GetSection("connectionStrings");
            if (conSection.ConnectionStrings[name] != null)
            {
                conSection.ConnectionStrings[name].ConnectionString = ConnectionString;
                conSection.ConnectionStrings[name].ProviderName = providerName;
                this.Save();
            }
            else
            {

                ConnectionStringSettings conSettings = new ConnectionStringSettings(name, ConnectionString, providerName);
                conSection.ConnectionStrings.Add(conSettings);
                this.Save();
            }
        }
        #endregion

        #region 刪除當(dāng)前或其他應(yīng)用程序配置文件中ConnectionString節(jié)點(diǎn)
        /// <summary>
        /// 定義刪除當(dāng)前或其他應(yīng)用程序配置文件中ConnectionString節(jié)點(diǎn)
        /// </summary>
        /// <param name="name">ConnectionStrings Name</param>
        /// <returns>刪除成功返回true,刪除失敗返回false</returns>
        public bool RemoveConnectionStringsSection(string name)
        {
            ConnectionStringsSection conSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
            if (conSection.ConnectionStrings[name] != null)
            {
                conSection.ConnectionStrings.Remove(name);
                this.Save();
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 保存配置文件,并重新賦值config為null
        /// <summary>
        /// 定義保存配置文件的方法
        /// </summary>
        public void Save()
        {
            config.Save();
            config = null;
        }
        #endregion

        #region 釋放配置文件對(duì)象
        /// <summary>
        /// 釋放配置文件對(duì)象
        /// </summary>
        public void Dispose()
        {
            if (config != null)
            {
                config.Save();
            }
        }
        #endregion
    }
    #endregion
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多