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

  • 相关阅读:
    C++各种进制的转换
    C++获取当前目录
    【转】Caffe初试(十)命令行解析
    libsvm下的windows版本中的工具的使用
    libsvm的数据格式及制作
    【转】Windows下使用libsvm中的grid.py和easy.py进行参数调优
    【转】Caffe初试(九)solver及其设置
    【转】Caffe初试(八)Blob,Layer和Net以及对应配置文件的编写
    【转】Caffe初试(七)其它常用层及参数
    Ubuntu 14.04 安装 Sublime Text 3
  • 原文地址:https://www.cnblogs.com/wintersun/p/1339430.html
Copyright © 2011-2022 走看看