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
}
}