zoukankan      html  css  js  c++  java
  • 简单实现自定义配置节1

           在程序中经常要使用自定义配置节来存储一些信息,下面是一个只读简单实现:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Configuration;

    namespace SmsWinForm
    {

        
    public class SectionHandler : System.Configuration.IConfigurationSectionHandler
        {
            
    public object Create(object parent, object configContext, System.Xml.XmlNode section)
            {
                
    return new Config(section);
            }
        }

        
    class Config
        {
            
    private System.Xml.XmlNode m_section;

            
    public Config(System.Xml.XmlNode node)
            {
                m_section 
    = node;
            }

            
    static private Config configSection
            {
                
    get
                {
                    Config config 
    = (Config)System.Configuration.ConfigurationManager.GetSection("SmsWinForm");
                    
    if (config == null)
                    {
                        
    throw new ApplicationException("Failed to get configuration from App.config.");
                    }
                    
    return config;
                }
            }

            
    public string this[string key]
            {
                
    get
                {
                    System.Xml.XmlNode node 
    = m_section.SelectSingleNode(key);
                    
    if (node != null)
                        
    return node.InnerText;
                    
    else
                        
    return null;
                }
            }

            
    static public string BoardID
            {
                
    get
                {
                    
    return configSection["boardid"];
                }
            }

            
    static public string CategoryID
            {
                
    get
                {
                    
    return configSection["categoryid"];
                }
            }

        }
    }

    App.config中
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      
    <configSections>
        
    <section name="SmsWinForm" type="SmsWinForm.SectionHandler,ConsoleTest1"/>
      
    </configSections>
      
    <SmsWinForm>
        
    <categoryid>CMPP</categoryid>
        
    <boardid>32</boardid>
      
    </SmsWinForm>
    </configuration>

    调用


    Tip:如果只是用于只读情况下,那这个方法足够了,并且大部分情况我们很少更新配置节。

    http://wintersun.cnblogs.com

  • 相关阅读:
    Eclipse 的快捷键
    using的用法
    二进制、八进制、十进制、十六进制之间的转换
    解决重置PostgreSQL 9.6密码的问题
    byte[]数组和int之间的转换
    【2021】IOS技术 UITableViewCell分割线无留白技巧
    【2021】IOS技术:属性观察器(Property Observers)倒计时实现
    【2019】问题记录一:后端获取URL参数的值内加号“+”变成空格“ ”
    【2015】对面向对象的理解
    “退格键”(Backspace)你想退到哪里?比较含退格的字符串
  • 原文地址:https://www.cnblogs.com/wintersun/p/1339430.html
Copyright © 2011-2022 走看看