.NET 配置文件简单使用

本文介绍了在.NET中使用配置文件的方法,包括使用appSettings、实现自定义配置节点以及使用Settings.settings等三种方式,并提供了详细的实现步骤。

      当我们开发系统的时候要把一部分设置提取到外部的时候,那么就要用到.NET的配置文件了。比如我的框架中使用哪个IOC容器需要可以灵活的选择,那我就需要把IOC容器的设置提取到配置文件中去配置。实现有几种方法。

1.使用appSettings

这个是最简单的可以设置和读取的用户设置

image

程序中可以用key去读取:

string objContainer = ConfigurationManager.AppSettings["objectContainer"];

简单实用但是不够优雅。

2.实现自己的配置节点

image

首先在configSections节点配置自己的配置解析类。

那么如何来解析这段配置呢?有两个办法。

方法1:

实现IConfigurationSectionHandler接口来自己解析配置文件的xml文件。

public class ObjectContainerElement
{
          public string Provider {get;set;}
          public string IocModule {get; set;}
}
 
public class AgileFRConfigurationHandler: IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, XmlNode section)
        {
               
               var node =section.ChildNodes[0];
                if (node.Name != "objectContainer")
                    throw new ConfigurationErrorsException("不可识别的配置项", node);
            var config = new ObjectContainerElement();
                foreach (XmlAttribute attr in node.Attributes)
                {
                    switch (attr.Name)
                    {
                        case "provider":
                            config. Provider = attr.Value;
                            break;
                        case "iocModule":
                            config .IocModule = attr.Value;
                            break;
                        default:
                            throw new ConfigurationErrorsException("不可识别的配置属性", attr);
                    }
                }
            }
            return config;
        }
 
//使用
 var config = ConfigurationManager.GetSection("agileFRConfiguration") as ObjectContainerElement;

这个方法看上去就略屌了,不过就是太麻烦了。

方法2:

继承ConfigurationSection类,配合ConfigurationProperty特性来实现

 public class ObjectContainerElement : ConfigurationElement
    {
        [ConfigurationProperty("provider", IsRequired = true)]
        public string Provider
        {
            get
            {
                return (string)this["provider"];
            }
            set
            {
                this["provider"] = (object)value;
            }
        }
        [ConfigurationProperty("iocModule", IsRequired = false)]
        public string IocModule
        {
            get
            {
                return (string)this["iocModule"];
            }
            set
            {
                this["iocModule"] = (object)value;
            }
        }
    }

 /// <summary>
    /// 配置处理类
    /// </summary>
    public class AgileFRConfigurationHandler : ConfigurationSection
    {
        [ConfigurationProperty("objectContainer", IsRequired = true)]
        public ObjectContainerElement ObjectContainer
        {
            get
            {
                return (ObjectContainerElement)this["objectContainer"];
            }
            set
            {
                this["objectContainer"] = (object)value;
            }
        }
    }
//使用
var configurationHandler = (AgileFRConfigurationHandler)ConfigurationManager.GetSection("agileFRConfiguration");
var objectContainer=configurationHandler.ObjectContainer;
 
这个方法简单优雅,我喜欢。
 

3.Settings.settings

这个方法我不太喜欢,它会自己生成配置文件对应的Class。不说了。

转载于:https://www.cnblogs.com/kklldog/p/3353018.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值