文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>ConfigManager.cs

ConfigManager.cs

时间:2010-08-21  来源:ha666

using System;
using System.Configuration;
using System.Web.Configuration;

namespace ThreadBase
{
    /// <summary>
    /// In charge of all system configuration
    /// </summary>
    public class ConfigManager
    {
        #region Private Property
        /// <summary>
        /// Dictionary of workingThreads
        /// </summary>
        //private static Dictionary<string, WorkingThreadCategory> workingThreads = new Dictionary<string, WorkingThreadCategory>();

        #endregion

        #region Public Properties
        /// <summary>
        /// Dragon DB connection string
        /// </summary>
        public static string DBConnString
        {
            get
            {
                string connString = null;
                try
                {
                    connString = WebConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;    //web config      
                }
                catch (Exception ex)
                {
                    connString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;  //service config
                }
                return connString;
            }
        }


        #endregion

        #region Constructor
        #endregion

        #region Public Methods
        /// <summary>
        /// Generic method to return a config object
        /// </summary>
        /// <typeparam name="T">Type of value to return</typeparam>
        /// <param name="sectionName">Config section name</param>
        /// <returns>Specified config object</returns>
        public static ConfigurationSection GetSection(string sectionName)
        {
            Configuration conf = null;

            try
            {
                conf = WebConfigurationManager.OpenWebConfiguration("~");    //web config      
            }
            catch (Exception ex)
            {
                conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  //service config
            }

            ConfigurationSection sec = conf.GetSection(sectionName);

            if (sec == null)
            {
                throw new ConfigurationErrorsException("Section:" + sectionName + " config is wrong or missing");
            }

            return sec;
        }

        
        /// <summary>
        /// Encrypt connection string in config file
        /// </summary>
        public static void EncryptConnString()
        {
            Configuration conf = null;

            try
            {
                conf = WebConfigurationManager.OpenWebConfiguration("~");    //web config      
            }
            catch (Exception ex)
            {
                conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  //service config
            }

            ConfigurationSection section = conf.GetSection("connectionStrings");
            if (!section.SectionInformation.IsProtected)
            {
                section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
                conf.Save();
            }
        }

        #endregion

    }
}

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载