标准配置文件
使用标准文件配置之前首先需要引用system.configuration
标准配置文件为App.config,在visual studio里点击该文件在文件属性里将复制到输出目录改为始终复制
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
<connectionStrings>
<add name="Quotation" connectionString="Data Source=jg-jiadong1;Initial Catalog=Quotation;Integrated Security=true;Timeout=360"/>
</connectionStrings>
<appSettings>
<add key="hBrokerUri" value="failover:(tcp://10.101.29.192:61636)"/>
<add key="dBrokerUri" value="failover:(tcp://10.17.41.104:61617)" />
<add key="qBrokerUri" value="failover:(tcp://10.101.237.60:61626)"/>
<add key="cBrokerUri" value="failover:(tcp://10.101.30.12:61616)"/>
<add key="mqMarketDataTopic" value="quotahq"/>
<add key="codes" value="600000.sh"/>
<add key="consoleShow" value="true"/>
<add key="consoleShowDetails" value="false"/>
<add key="logToDatabase" value="false"/>
<add key="numOfReservedDays" value="3"/>
<add key="cleanUpTime" value="0800"/>
</appSettings>
</configuration>
这是一个典型的配置文件格式,里面只用到了默认提供的两个配置标签。
<appSettings>与<connectionStrings>是配置文件标准标签,用如下方法取出配置值
var codes = ConfigurationManager.AppSettings["codes"];
var connectionString = ConfigurationManager.ConnectionStrings["Quotation"].ConnectionString;
更复杂的配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MySection111" type="RwConfigDemo.MySection1, RwConfigDemo" />
<section name="MySection222" type="RwConfigDemo.MySection2, RwConfigDemo" />
<section name="MySection333" type="RwConfigDemo.MySection3, RwConfigDemo" />
<section name="MySection444" type="RwConfigDemo.MySection4, RwConfigDemo" />
</configSections>
<MySection111 username="fish-li" url="http://www.cnblogs.com/fish-li/"></MySection111>
<MySection222>
<users username="fish" password="liqifeng"></users>
</MySection222>
<MySection444>
<add key="aa" value="11111"></add>
<add key="bb" value="22222"></add>
<add key="cc" value="33333"></add>
</MySection444>
</configuration>
注意到这里有3种不同类型的自定义配置,每种不同类型的配置要添加不同的类。每种配置首先要在<configSections>里进行注册。
<section name="MySection111" type="RwConfigDemo.MySection1, RwConfigDemo" />
//name属性表示这个配置节的名称,type属性表示这个属性(类的位置,类所在的程序集名称)
<MySection111 username="fish-li" url="http://www.cnblogs.com/fish-li/"></MySection111> //读取配置 var myConfigurationSection1 = (MySection1)ConfigurationManager.GetSection("MySection111"); myConfigurationSection1.UserName; myConfigurationSection1.Url; //配置类的写法 public class MySection1 : ConfigurationSection { [ConfigurationProperty("username", IsRequired=true)] public string UserName { get { return this["username"].ToString();} set { this["username"] = value;} } [ConfigurationProperty("url", IsRequired=true] public string Url { get { return this["url"].ToString();} set { this["url"] = value;} }<MySection222> <users username="fish" password="liqifeng"></users> </MySection222> //读取配置 var myConfigurationSection2 = (MySection2) ConfigurationManager.GetSection("MySection222"); myConfigurationSection2.Users.UserName; myConfigurationSection2.Users.Password; //配置类写法 public class MySection2: ConfigurationSection { [ConfigurationProperty("users", IsRequired=true)] public MySectionElement Users { get { return (MySectionElement)this["users"];} } } public class MySectionElement: ConfigurationElement { [ConfigurationProperty("username", IsRequired=true)] public string UserName { get { return this["username"].ToString();} set { this["username"] = value;} } [ConfigurationProperty("password", IsRequired=true)] public string Password { get { return this["password"].ToString();} set { this["password"] = value;} } }//注意这里的Collection配置组需要一个单独的Tag <MySection444> <MyTag> <add key="aa" value="11111"></add> <add key="bb" value="22222"></add> <add key="cc" value="33333"></add> <MyTag> </MySection444> //读取配置 foreach (MyKeyValueSetting kv in myConfigurationSection4.KeyValues) { Console.WriteLine(kv.Key); } 或者 var v = from MyKeyValueSetting kv in myConfigurationSection4.KeyValues where kv.Key == "aa" select kv; foreach (var t in v) { Console.WriteLine(t.Key); } // 注意这两种写法中都要明确的写出kv的变量类型 //配置类写法 public class MySection4: ConfigurationSection { [ConfigurationProperty("MyTag", IsRequired = true)] [ConfigurationCollection(typeof(MyKeyValueCollection))] public MyKeyValueCollection KeyValues { get { return (MyKeyValueCollection)base["MyTag"]; } } } //[ConfigurationCollection(typeof(MyKeyValueSetting))] 这个可写可不写 public class MyKeyValueCollection: ConfigurationElementCollection { public MyKeyValueCollection(): base(StringComparer.OrdinalIgnoroCase) { } new public MyKeyValueSetting this[string name] { get { return (MyKeyValueSetting)base.BaseGet(name); } } protected override ConfigurationElement CreateNewElement() { return new MyKeyValueSetting(); } protected override object GetElementKey(ConfigurationElement element) { return ((MyKeyValueSetting)element).Key; } } public class MyKeyValueSetting: ConfigurationElement { [ConfigurationProperty("key", IsRequired=true] public string Key { get { return this["key"].ToString();} set { this["key"] = value;} } [ConfigurationProperty("value", IsRequired=true)] public string Value { get {return this["key"].ToString();} set {this["value"] = value; } }