web.config文件自定义配置节的使用方法 (转)
来源:互联网 发布:淘宝全屏图片轮播制作 编辑:程序博客网 时间:2024/06/05 11:07
转自:http://blog.joycode.com/yaodong/articles/25509.aspx
web.config文件自定义配置节的使用方法
web.config文件自定义配置节的使用方法的一个简单例子
用来演示的程序名为MyApp,Namespace也是MyApp
1。编辑web.config文件
添加以下内容,声明一个Section
声明了一个叫AppConfig的Section
2。编辑web.config文件
添加以下内容,加入一个Section
这个Section包括两个 Key
3。从IConfigurationSectionHandler派生一个类,AppConfig
实现Create方法,代码如下
public class AppConfig : IConfigurationSectionHandler
{
static String m_connectionString = String.Empty;
static Int32 m_userCount = 0;
public static String ConnectionString
{
get
{
return m_connectionString;
}
}
public static Int32 UserCount
{
get
{
return m_userCount;
}
}
static String ReadSetting(NameValueCollection nvc, String key, String defaultValue)
{
String theValue = nvc[key];
if(theValue == String.Empty)
return defaultValue;
return theValue;
}
public object Create(object parent, object configContext, XmlNode section)
{
NameValueCollection settings;
try
{
NameValueSectionHandler baseHandler = new NameValueSectionHandler();
settings = (NameValueCollection)baseHandler.Create(parent, configContext, section);
}
catch
{
settings = null;
}
if ( settings != null )
{
m_connectionString = AppConfig.ReadSetting(settings, "ConnectionString", String.Empty);
m_userCount = Convert.ToInt32(AppConfig.ReadSetting(settings, "UserCount", "0"));
}
return settings;
}
}
我们把所有的配置都映射成相应的静态成员变量,并且是写成只读属性,这样程序通过
类似AppConfig.ConnectionString就可以访问,配置文件中的项目了
4。最后还要做一件事情
在Global.asax.cs中的Application_Start中添加以下代码
System.Configuration.ConfigurationSettings.GetConfig("AppConfig");
这样在程序启动后,会读取AppConfig这个Section中的值,系统会调用你自己实现的IConfigurationSectionHandler接口来读取配置
- web.config文件自定义配置节的使用方法 (转)
- web.config文件自定义配置节的使用方法
- web.config文件自定义配置节的使用方法
- web.config文件自定义配置节的使用方法
- web.config文件自定义配置节的使用方法
- web.config文件自定义配置节的使用方法
- web.config文件自定义配置节的使用方法的一个简单例子
- web.config文件自定义配置节的使用方法的一个简单例子
- web.config文件的配置
- 在web.config和app.config文件中增加自定义配置节点的方法
- 自定义web.config配置节(分析)
- 在web.config里自定义配置节
- Web.Config文件配置
- web.config文件配置
- 改web.config文件的pages配置节
- web.config文件的配置 作
- web.config文件配置的一些说明
- 如何在.config文件自定义配置节
- 【整理】Android中使用XML自定义组件各种状态下的背景图片
- HR--Copy of fetching HR data in different ways
- .NET 框架中使用配置文件(转)
- 如何编程操作Web.config文件中的数据
- Web.Config文件详解(转)
- web.config文件自定义配置节的使用方法 (转)
- 在网页中使用Socket编程
- 惟帆K300A ADSL Modem路由设置
- ASP.NET页面之间传递参数的几种方法荟萃(转)
- IIS 服务器端include指令
- 创建Active控件
- JavaScript调用C#代码
- 在多线程中调用WinForm(C#) (转载)
- .NET自定义控件——与属性相关的设计时特性