C# EXE.CONFIG应用程序配置文件
2004-11-15 22:49
447 查看
类似RemotingConfiguration.Configure("SyncAsync.exe.config");
applicationname.exe.config 你可以通过编程的方式,使用SYSTEM.CONFIGURATION名称空间从
XML .config文件中获取应用程序设置信息。
例子代码:
<appSettings>
<!--add key="constring" value="Server=zsl;uid=sa;pwd=;DataBase=OA"-->
<add key="constring" value="Provider=sqloledb;Data Source=zsl;Initial Catalog=OA;User Id=sa;Password=;"/>
</appSettings>
ConfigurationSettings.AppSettings["constring"].ToString()
只是在程序开始时验证,另外在文件中含有脚本文件,CDATA文件如何解决?
web.config文件自定义配置节的使用方法- -
[转载]web.config文件自定义配置节的使用方法
web.config文件自定义配置节的使用方法的一个简单例子
用来演示的程序名为MyApp,Namespace也是MyApp
1。编辑web.config文件
添加以下内容,声明一个Section
<configSections>
<section name="AppConfig" type="MyApp.AppConfig, MyApp" />
</configSections>
声明了一个叫AppConfig的Section
2。编辑web.config文件
添加以下内容,加入一个Section
<AppConfig>
<add key="ConnectionString" value="this is a ConnectionString" />
<add key="UserCount" value="199" />
</AppConfig>
这个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接口来读取配置
applicationname.exe.config 你可以通过编程的方式,使用SYSTEM.CONFIGURATION名称空间从
XML .config文件中获取应用程序设置信息。
例子代码:
<appSettings>
<!--add key="constring" value="Server=zsl;uid=sa;pwd=;DataBase=OA"-->
<add key="constring" value="Provider=sqloledb;Data Source=zsl;Initial Catalog=OA;User Id=sa;Password=;"/>
</appSettings>
ConfigurationSettings.AppSettings["constring"].ToString()
只是在程序开始时验证,另外在文件中含有脚本文件,CDATA文件如何解决?
web.config文件自定义配置节的使用方法- -
[转载]web.config文件自定义配置节的使用方法
web.config文件自定义配置节的使用方法的一个简单例子
用来演示的程序名为MyApp,Namespace也是MyApp
1。编辑web.config文件
添加以下内容,声明一个Section
<configSections>
<section name="AppConfig" type="MyApp.AppConfig, MyApp" />
</configSections>
声明了一个叫AppConfig的Section
2。编辑web.config文件
添加以下内容,加入一个Section
<AppConfig>
<add key="ConnectionString" value="this is a ConnectionString" />
<add key="UserCount" value="199" />
</AppConfig>
这个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接口来读取配置
相关文章推荐
- 用C#实现WEB浏览器
- 用C#给程序加启动画面并只允许一个应用程序实例运行
- C#读写注册表
- 应该使用c# 预定义类型 还是绝对不要使用预定义类型。
- 蛙蛙推荐:c#书写规范.doc
- 蛙蛙推荐:C#编码规范.doc
- 用C#给程序加启动画面并只允许一个应用程序实例运行
- 在C#中使用DLL文件
- 请问在C#中如何将最小化后的窗口从Windows的状态栏上消失?
- C#里面有什么办法能方便的定义Field和Property
- 使用C#编写Ice应用程序
- Inside c#(Introduction)_1
- 使用C#在进度条中显示复制文件的进度
- C#如何获得cpu,硬盘的物理序列号
- 使用C#在进度条中显示复制文件的进度
- Inside c#(Foreword)_2
- C#多线程共享数据
- c#中的套打
- 在C#中应用HOOK
- C#钩子本线程内消息拦截