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

推荐订阅源

V
Visual Studio Blog
量子位
大猫的无限游戏
大猫的无限游戏
Hugging Face - Blog
Hugging Face - Blog
S
SegmentFault 最新的问题
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
Google DeepMind News
Google DeepMind News
小众软件
小众软件
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
雷峰网
雷峰网
Jina AI
Jina AI
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 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的值被修改.