按照MSDN描述 System.Configuration 命名空间 包含处理配置信息的类型
本篇文章主要两方面的内容
1. 如何使用ConfigurationManager 读取AppSetting和ConnectionStrings
2. 如何使用自定义 Section,我这里的自定义Section格式为
<SectionName>
<services>
服务1的描述信息,供IoC容器使用
服务2的描述信息,供IoC容器使用
。。。
</services>
</SectionName>
其实如果读者使用一些比较出名的IoC 框架(Unity)都会有配置管理,根本不用自定义Section,但是技多不压身,多了解点东西总是没错的。
一、ConfigurationManager 类
提供对客户端应用程序配置文件的访问。无法继承此类
命名空间: System.Configuration
程序集: System.Configuration.dll
继承关系:
原型定义:public static class ConfigurationManager
由上面的定义可以看出ConfigurationManager是一个静态类
静态属性:
1. ConfigurationManager.AppSettings 获取当前应用程序默认配置的 AppSettingsSection 数据
原型定义:public static NameValueCollection AppSettings { get; }
从上面的定义可以看出 AppSetting 是一个 键值对
2. ConfigurationManager.ConnectionStrings 获取当前应用程序默认配置的 ConnectionStringsSection 数据
例子:
app.config
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v2.0.50727"/>
</startup> <connectionStrings>
<add name="KKSEntities" connectionString="metadata=res://*/DbModel.csdl|res://*/DbModel.ssdl|res://*/DbModel.msl;provider=System.Data.SqlClient;provider connection string="data source=XXX.XXX.XXX.XXX;initial catalog=KKS;user id=XXXX;password=KKKKK;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings> <appSettings>
<add key="Aphasia" value="www.cnblogs.com/Aphasia"/>
</appSettings>
</configuration>
Program.cs
using System;
using System.Configuration; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string web = ConfigurationManager.AppSettings["Aphasia"];
Console.WriteLine(web); string cnstr = ConfigurationManager.ConnectionStrings["KKSEntities"].ConnectionString;
Console.WriteLine(cnstr); }
}
}
效果:
二、 自定义Section
app.config
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="ServicesSection" type="CustomDemo.ServicesSection,CustomDemo"/>
</configSections> <ServicesSection>
<services>
<add ServiceName="XXX服务1" Impl="XXX.Impl.XXXService1" Inter="XXX.Inter.XXXInter1" />
<add ServiceName="XXX服务2" Impl="XXX.Impl.XXXService2" Inter="XXX.Inter.XXXInter2" />
<add ServiceName="XXX服务3" Impl="XXX.Impl.XXXService3" Inter="XXX.Inter.XXXInter3" />
</services>
</ServicesSection> <startup>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
Program.cs
using System;
using System.Configuration; namespace CustomDemo
{
class Program
{
static void Main(string[] args)
{
ServicesSection myServices = ConfigurationManager.GetSection("ServicesSection") as ServicesSection;
foreach (Service item in myServices.ServiceItems)
{
Console.WriteLine("ServiceName:{0} Impl:{1} Inter:{2}", item.ServiceName, item.Impl, item.Inter);
}
}
} #region[自定义Section处理代码]
public class Service : ConfigurationElement
{
#region[当遇不能识别的元素时不让程序报错]
protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
{
return true;
}
protected override bool OnDeserializeUnrecognizedElement(string elementName, System.Xml.XmlReader reader)
{
return true;
}
#endregion #region[节点元素]
[ConfigurationProperty("ServiceName", IsRequired = true)]
public string ServiceName
{
get { return this["ServiceName"].ToString(); }
} [ConfigurationProperty("Impl", IsRequired = true)]
public string Impl
{
get { return this["Impl"].ToString(); }
} [ConfigurationProperty("Inter", IsRequired = true)]
public string Inter
{
get { return this["Inter"].ToString(); }
}
#endregion
} public class Services : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new Service();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Service)element).ServiceName;
}
} public class ServicesSection : ConfigurationSection
{
[ConfigurationProperty("services", IsDefaultCollection = false)]
public Services ServiceItems { get { return (Services)base["services"]; } }
}
#endregion
}
效果:
本小节代码下载
三、参考资料