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

推荐订阅源

月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
大猫的无限游戏
大猫的无限游戏
爱范儿
爱范儿
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Register - Security
The Register - Security
IT之家
IT之家
博客园_首页
Microsoft Security Blog
Microsoft Security Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
I
InfoQ
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Jina AI
Jina AI
Apple Machine Learning Research
Apple Machine Learning Research
M
MIT News - Artificial intelligence
博客园 - Franky
C
Check Point Blog
T
The Blog of Author Tim Ferriss
V
Visual Studio Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Tailwind CSS Blog
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
美团技术团队
The Cloudflare Blog
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
V
V2EX
aimingoo的专栏
aimingoo的专栏
GbyAI
GbyAI
G
Google Developers Blog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42
罗磊的独立博客
量子位
MongoDB | Blog
MongoDB | Blog
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
D
Docker
人人都是产品经理
人人都是产品经理

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