您的位置:首页 > 其它

读取配置config文件

2016-04-26 18:16 405 查看
<system.net>
<mailSettings>
<smtp from="Fish.Q.Li@newegg.com">
<network />
</smtp>
</mailSettings>
</system.net>


读取上面↑的xml代码{

mtpSection section = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
labMailFrom.Text = "Mail From: " + section.From;


}

写入配置文件:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

SmtpSection section = config.GetSection("system.net/mailSettings/smtp") as SmtpSection;
section.From = "Fish.Q.Li@newegg.com2";

config.Save();


如果配置文件为以下格式的:

<MySection444>
<add key="aa" value="11111"></add>
<add key="bb" value="22222"></add>
<add key="cc" value="33333"></add>
</MySection444>


读取方法:

<sinosoft>
<esb>
<headers>
<children>
<header service="s-1" version="1" consumer="CS-1/65/" password="12436798" desc=""></header>
<header service="s-2" version="1" consumer="CS-1/6" password="4235465687" desc=""></header>
<header service="s-3" version="1" consumer="CS-1/-01" password="24346" desc=""></header>

</children>
</headers>
</esb>
<reports>
<sms url="" userid="TCBB" password="456"></sms>
</reports>
</sinosoft>


View Code
小结:
1. 为每个集合中的参数项创建一个从ConfigurationElement继承的派生类。
2. 为集合创建一个从ConfigurationElementCollection继承的集合类,具体在实现时主要就是调用基类的方法。
3. 在创建ConfigurationSection的继承类时,创建一个表示集合的属性就可以了,注意[ConfigurationProperty]的各参数。

参考自:http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html#_labelStart
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: