C#中读写自定义的web 配置文件
开发程序的过程中,有时候我们需要自己编写一个config文件,比如取名App.config, 然后写一些配置信息在里面。然后我们需要编写C#代码来对这个配置文件进行读写
比如:App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="mySection" type="test.MySection, test"/>
</configSections>
<mySection>
<add key="user1" value="aaa111" />
<add key="user2" value="bbb111" />
</mySection>
</configuration>
可以看出在这个配置文件中,我们自己定义了一个section => mySection,这里自定义的section都需要从ConfigurationSection中继承, 所以我们写一个MySection类,从ConfigurationSection中继承
using System;
using System.Configuration; public class MySection : ConfigurationSection // 所有配置节点都要选择这个基类
{
private static ConfigurationProperty s_property = new ConfigurationProperty(string.Empty, typeof(MyKeyValueCollection), null, ConfigurationPropertyOptions.IsDefaultCollection); [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
public MyKeyValueCollection KeyValues {
get {
return (MyKeyValueCollection)base[s_property];
}
}
[ConfigurationCollection(typeof(MyKeyValueSetting))]
public class MyKeyValueCollection : ConfigurationElementCollection // 自定义一个集合
{
// 基本上,所有的方法都只要简单地调用基类的实现就可以了。
public MyKeyValueCollection()
: base(StringComparer.OrdinalIgnoreCase) // 忽略大小写
{ } // 其实关键就是这个索引器。但它也是调用基类的实现,只是做下类型转就行了。
new public MyKeyValueSetting this[string name] {
get { return (MyKeyValueSetting)base.BaseGet(name); }
set { base[name] = value; }
} // 下面二个方法中抽象类中必须要实现的。
protected override ConfigurationElement CreateNewElement() {
return new MyKeyValueSetting();
} protected override object GetElementKey(ConfigurationElement element) {
return ((MyKeyValueSetting)element).Key;
} // 说明:如果不需要在代码中修改集合,可以不实现Add, Clear, Remove
public void Add(MyKeyValueSetting setting) {
this.BaseAdd(setting);
} public void Clear() {
base.BaseClear();
} public void Remove(string name) {
base.BaseRemove(name);
}
}
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["value"].ToString(); }
set { this["value"] = value; }
}
} }
读取App.config file中的section
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var mySection = config.GetSection("mySection") as MySection;
foreach (MySection.MyKeyValueSetting add in mySection.KeyValues) {
Console.WriteLine(string.Format("{0}-{1}", add.Key, add.Value));
}
往App.config中写section
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var mySection = config.GetSection("mySection") as MySection; mySection.KeyValues.Clear();
mySection.KeyValues.Add(new MySection.MyKeyValueSetting() { Key = "aaaaaa", Value = "ddddddd" }); config.Save();
ConfigurationManager.RefreshSection("mySection"); //刷新
C#中读写自定义的web 配置文件的更多相关文章
- 在.net中读写config文件的各种方法(自定义config节点)
http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html 阅读目录 开始 config文件 - 自定义配置节点 config文件 - ...
- [转]通过继承ConfigurationSection,在web.config中增加自定义配置
本文转自:http://www.blue1000.com/bkhtml/2008-02/55810.htm 前几天写了一篇使用IConfigurationSectionHandler在web.conf ...
- 在配置文件(.settings、.config)中存储自定义对象
原文:在配置文件(.settings..config)中存储自定义对象 引言 我前面曾写过一篇<使用配置文件(.settings..config)存储应用程序配置>,我在其中指出“sett ...
- C#读写自定义的多字段配置文件
mark一下,日后填坑 参考: WPF 读写自己写的配置文件
- 在.net中读写config文件的各种方法
阅读目录 开始 config文件 - 自定义配置节点 config文件 - Property config文件 - Element config文件 - CDATA config文件 - Collec ...
- 在.net中读写config文件的各种方法【转】
今天谈谈在.net中读写config文件的各种方法. 在这篇博客中,我将介绍各种配置文件的读写操作. 由于内容较为直观,因此没有过多的空道理,只有实实在在的演示代码, 目的只为了再现实战开发中的各种场 ...
- 重新想象 Windows 8.1 Store Apps (90) - 通信的新特性: 通过 HttpBaseProtocolFilter 实现 http 请求的缓存控制,以及 cookie 读写; 自定义 HttpFilter; 其他
[源码下载] 重新想象 Windows 8.1 Store Apps (90) - 通信的新特性: 通过 HttpBaseProtocolFilter 实现 http 请求的缓存控制,以及 cooki ...
- 自定义WCF的配置文件
原文地址:http://www.cnblogs.com/shanyou/archive/2008/12/02/1346298.html WCF的承载既可以通过编码实现,也能够通过配置实现.而且使用配置 ...
- Spring Boot中的自定义start pom
start pom是springboot中提供的简化企业级开发绝大多数场景的一个工具,利用好strat pom就可以消除相关技术的配置得到自动配置好的Bean. 举个例子,在一般使用中,我们使用基本的 ...
随机推荐
- android OTA升级包制作【转】
本文转载自:http://www.thinksaas.cn/topics/0/445/445670.html 0.签名 java -Xmx2048m -jar out/host/linux-x86/f ...
- poj 2251 Dungeon Master-搜索进阶-暑假集训
普及一下知识 s.empty() 如果栈为空返回true,否则返回falses.size() 返回栈中元素的个数s.pop() 删除栈顶元素但不返回其值s.top() 返回栈顶的元素,但不删除该元素s ...
- Java 面试题问与答:编译时与运行时
Java 面试题问与答:编译时与运行时 2012/12/17 | 分类: 基础技术, 职业生涯 | 5 条评论 | 标签: RUNTIME, 面试 分享到:58 本文作者: ImportNew - 朱 ...
- JavaWeb -- 服务器传递给Servlet的对象 -- ServletConfig, ServletContext,Request, Response
1. ServletConfig 有一些东西不合适在程序中写死,应该写在web.xml中,比如 文字怎么显示, 访问数据库名 和 密码, servlet要读取的配置文件 等等.. l在Servle ...
- codevs1218 疫情控制
疫情控制(blockade.cpp/c/pas)[问题描述]H 国有 n 个城市,这 n 个城市用 n-1 条双向道路相互连通构成一棵树,1 号城市是首都,也是树中的根节点.H 国的首都爆发了一种危害 ...
- MVC中使用showModalDialog
1.mvc中使用模态对话框用于修改数据,如果第一次修改过后刷新页面,第二次修改时显示内容依然是第一次修改之前的,这里用js中的Math.Random()解决 Views: <%: Html.Ac ...
- 分享知识-快乐自己:ActiveMQ 安装部署
1):下载 ActiveMQ tar 包 2):上传到 服务器中 3):解压到 指定目录中 [root@admin tools]# tar -zxvf apache-activemq-5.2.0-bi ...
- WCF之契约的分类(部分為參考他人)
什么是契约呢?在使用WCF时,对其制定各种各样的规则,就叫做WCF契约.任何一个分布式的应用程序在传递消息的时候都需要实现制定一个规则. 任何一个分布式应用程序,它之所以能够互相传递消息,都是事先制定 ...
- windows 2013 datacenter 安装sql server2008 r2兼容性
add-windowsfeature RSAT-Clustering-AutomationServer
- [CERC 2008] Suffix reconstruction
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4319 [算法] 首先 , 我们可以求出这个字符串的rank数组 按照SA逐位枚举 , ...