惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
美团技术团队
博客园_首页
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
B
Blog
D
DataBreaches.Net
腾讯CDC
C
Check Point Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42
月光博客
月光博客
V
V2EX
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
The Cloudflare Blog
博客园 - 叶小钗
Y
Y Combinator Blog

博客园 - DingJun

Javascript 继承方法3 Javascript 继承方法2 Javascript 继承方法1 Call web service from excel Cannot load type (加载页面出错) 几个主流的浏览器引擎及判定 防止数据库日志文件增长 配置发布数据库服务器时碰到错误18483 一些有关。NET界面处理与多线程的文章 不可恢复的生成错误 在.Net安装项目中如何判断操作系统的版本 修改dataConfiguration.config文件 SQL Server 使用外部连接 在.NET下利用目录服务操纵本机用户和用户组 System.windows.forms.datagrid控件使用技巧 区域设置与格式化(1) 默认的 IIS MIME 类型关联 在.NET中使用XPath查找指定元素时遇到的麻烦(以dataConfiguration.config为例) 使用Data access block
读取配置文件中的自定义节
DingJun · 2005-07-28 · via 博客园 - DingJun

用户可以在配置文件中添加自定义节,但添加的自定义节也必须由用户编写自定义类来读取,该自定义类必须实现IConfigurationSectionHandler 接口,一个简单的示例代码如下:

自定义类:

using System;
using System.Xml;
using System.Collections;
using System.Configuration;
using System.Collections.Specialized; 

namespace WebApp
{
    
/// <summary>
    
/// Summary description for MyConfiguration.
    
/// </summary>

    public class MyConfiguration : IConfigurationSectionHandler
    
{
        
public static string Name;
        
public static string Password;

        
public MyConfiguration()
        
{
            
        }


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

            
else
            
{
                Name 
= settings["Name"];
                Password 
= settings["Password"];
            }

            
return settings;
        }

    }

}

相应的配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
<configSections>
            
<section name="WebAppConfiguration" type="WebApp.MyConfiguration, WebApp" />
    
</configSections>
    
    
<WebAppConfiguration>
        
<add key="Name" value="root" />
        
<add key="Password" value="MyRoot" />
    
</WebAppConfiguration>
</configuration>

读取配置文件的示例代码:

private void Page_Load(object sender, System.EventArgs e)
        
{
            System.Configuration.ConfigurationSettings.GetConfig(
"WebAppConfiguration");
            Response.Write(MyConfiguration.Name 
+ "," + MyConfiguration.Password);
        }

说明: 在System.Configuration.ConfigurationSettings.GetConfig("WebAppConfiguration");语句被执行时,MyConfiguration类的Creat方法将被Framework自动调用,MyConfiguration.Name和MyConfiguration.Password的值被修改.