而在.net2.0中使用ConfigurationManager 和WebConfigurationManager 类可以很好的管理配置文件,ConfigurationManager类在System.Configuration 中,WebConfigurationManager在System.Web.Configuration中。 根据MSDN的解释,对于 Web 应用程序配置,建议使用 System.Web.Configuration.WebConfigurationManager 类,而不要使用 System.Configuration.ConfigurationManager 下面我给出一个简单的例子说明如何使用WebConfigurationManager操作配置文件: //打开配置文件 Configuration config = WebConfigurationManager.OpenWebConfiguration " value="key2's value" /> </appSettings> 修改和删除节点或属性也非常方便: //打开配置文件 Configuration config = WebConfigurationManager.OpenWebConfiguration
对于WINFORM程序,使用 System.Configuration.ConfigurationManager; 对于ASP.NET 程序, 使用 System.Web.Configuration.WebConfigurationManager 读取值: Asp.Net: System.Web.Configuration.WebConfigurationManager.AppSettings[“y”]; WinForm: System.Configuration.ConfigurationManager.AppSettings 添加一项 ASP.NET(需要有写权限): Configuration config = WebConfigurationManager.OpenWebConfiguration(null); AppSettingsSection 修改一项 Asp.Net Configuration config = WebConfigurationManager.OpenWebConfiguration(null); AppSettingsSection 删除一项 Asp.Net Configuration config = WebConfigurationManager.OpenWebConfiguration(null); AppSettingsSection
#region 菜单结构构建 ButtonGroup bg = new ButtonGroup(); string websiteUrl = WebConfigurationManager.AppSettings = MenuHelper.GetMenuUrl("Weixin/Index"), url = string.Format("{0}/{1}", websiteUrl, WebConfigurationManager.AppSettings openId={1}", WebConfigurationManager.AppSettings["mainPage"], result.openid), UriKind.Relative); Log.Logger.Error("OAuth2授权失败:", e); response.Headers.Location = new Uri(WebConfigurationManager.AppSettings
WebConfig { public static readonly string ConnectionString = System.Web.Configuration.WebConfigurationManager.AppSettings //public static string ConnectionString() //{ // return System.Web.Configuration.WebConfigurationManager.AppSettings public static int DataBaseType() { string show = System.Web.Configuration.WebConfigurationManager.AppSettings public static bool isShowErrorSQL() { string show = System.Web.Configuration.WebConfigurationManager.AppSettings
class/System.Configuration/System.Configuration/Configuration.cs:261 at System.Web.Configuration.WebConfigurationManager.GetSection /profiles/mono-mac-xamarin/build-root/mono-3.0.10/mcs/class/System.Web/System.Web.Configuration_2.0/WebConfigurationManager.cs :504 at System.Web.Configuration.WebConfigurationManager.GetSection (System.String sectionName) [0x00006 /profiles/mono-mac-xamarin/build-root/mono-3.0.10/mcs/class/System.Web/System.Web.Configuration_2.0/WebConfigurationManager.cs
WeiXinController : Controller{ public static readonly string appid = System.Web.Configuration.WebConfigurationManager.AppSettings ["wxappid"]; public static readonly string secret = System.Web.Configuration.WebConfigurationManager.AppSettings ["wxsecret"]; public static readonly bool isDedug = System.Web.Configuration.WebConfigurationManager.AppSettings
public ActionResult SiteConfig() { SiteConfig _siteConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration SiteConfig") as Ninesky.Core.Config.SiteConfig; return View(_siteConfig); } 代码很简单,利用WebConfigurationManager SiteConfig(FormCollection form) { SiteConfig _siteConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration }); } else return View(_siteConfig); } } 代码也非常简单,与读取配置文件相同,使用WebConfigurationManager
HttpContext.Current.Request.ApplicationPath) { } public ConfigurationOperator(string path) { config = WebConfigurationManager.OpenWebConfiguration
web.Config中的指定节 private void ProtectSection(string sectionName) { Configuration config = WebConfigurationManager.OpenWebConfiguration web.Config中的指定节 private void UnProtectSection(string sectionName) { Configuration config = WebConfigurationManager.OpenWebConfiguration
summary> /// 密钥 /// static string key = System.Web.Configuration.WebConfigurationManager.AppSettings lesschat日志频道Incoming WebHook地址 /// static string url = System.Web.Configuration.WebConfigurationManager.AppSettings
下面记录一些管理配置文件的姿势: ConfigurationManager用于在客户机应用程序中获取配置信息;对于web项目,请使用WebConfigurationManager类。
其他应用程序的虚拟路径</param> public ConfigurationOperator(string path) { config = WebConfigurationManager.OpenWebConfiguration
string.Format("{0}/template.pdf", System.Web.HttpContext.Current.Request.MapPath(System.Web.Configuration.WebConfigurationManager.AppSettings
CustomErrorsSection; //这样获取也可以,但是要注意里面的路径的写法 CustomErrorsSection CES = (CustomErrorsSection)WebConfigurationManager.OpenWebConfiguration
object sender, EventArgs e, AsyncCallback cb, object state) { string connect = WebConfigurationManager.ConnectionStrings
用工厂返回实例 public class DALFactory { public static string cnString = System.Web.Configuration.WebConfigurationManager.AppSettings
string author, string publisher, DateTime pubDate, decimal price) { string connString = WebConfigurationManager.ConnectionStrings ["SiteDBConnection"].ConnectionString; string provider = WebConfigurationManager.ConnectionStrings
protected void Button1_Click(object sender,EventArgs e) { string connString = WebConfigurationManager.AppSettings
IDbConnection OpenConnection() 20: { 21: IDbConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings
protected void Button1_Click(object sender, EventArgs e) { string connString = WebConfigurationManager.AppSettings