读取App.config自定义标签的值
一:程序截图
二:具体代码
config配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MySection111" type="configToRead.MySection1,configToRead" />
<section name="MySection444" type="configToRead.MySection4, configToRead" />
</configSections>
<MySection111 username="红马車" url="http://www.cnblogs.com/hongmaju/"></MySection111> <MySection444>
<add key="aa" value=""></add>
<add key="bb" value=""></add>
<add key="cc" value=""></add>
</MySection444>
</configuration>
要在程序中使用自定义配置我们还需要实现存取这个配置块的类型,一般需要做如下三件事:
1. 定义类型从System.Configuration.ConfigurationSection继承
2. 定义配置类的属性,这些属性需要用ConfigurationProperty特性修饰,并制定属性在配置节中的名称和其他一些限制信息
3. 通过基类的string索引器实现属性的get ,set
MySection1.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration; namespace configToRead
{
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; }
}
}
}
MySection4.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration; namespace configToRead
{
public class MySection4 : ConfigurationSection // 所有配置节点都要选择这个基类
{
private static readonly 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);
}
} // 下面二个方法中抽象类中必须要实现的。
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; }
}
}
}
前台代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using configToRead;
using System.Configuration;
using System.Net.Configuration;
using System.Collections.Specialized; namespace configToRead
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
MySection1 mySectioin1 = (MySection1)ConfigurationManager.GetSection("MySection111");
textBox1.Text = mySectioin1.UserName;
textBox1.Text += mySectioin1.Url; MySection4 mySection4 = (MySection4)ConfigurationManager.GetSection("MySection444");
txtKeyValues.Text = string.Join("\r\n",
(from kv in mySection4.KeyValues.Cast<MyKeyValueSetting>()
let s = string.Format("{0}={1}", kv.Key, kv.Value)
select s).ToArray()); } private void Form1_Load(object sender, EventArgs e)
{
MySection4 mySection4 = (MySection4)ConfigurationManager.GetSection("MySection444");
comboBox1.Items.AddRange((from kv in mySection4.KeyValues.Cast<MyKeyValueSetting>()
let s = string.Format("{0}", kv.Key)
select s).ToArray());//linqToSql
}
}
}
三:报错解决
将App.config包含到项目即可
读取App.config自定义标签的值的更多相关文章
- C# 读取app.config配置文件节点键值,提示"配置系统未能初始化" 错误的解决方案
MSDN里写到, 如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素. 将自己添加的appSettin ...
- 【Core】.NET Core中读取App.config配置文件
1.项目中添加App.config文件 因为.NET Core的项目本质是控制台应用,所以ConfigurationManager的API会去默认读取app.config配置文件,而不是web.con ...
- 一个web.Config或app.Config自定义段configSections的示例
一个web.Config或app.Config自定义段configSections的示例 越来越觉得,直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml ...
- C# App.config 自定义 配置节 出现的问题:配置系统未能初始化
C# 读取app.config配置文件 节点键值,提示 "配置系统未能初始化" 错误的解决方案 新建C#项目,在app.config中添加了appSettings项,运行时出现&q ...
- [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法
本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...
- C#读取App.config/Web.config
读取需要添加 System.Configuration 引用, 两种方式添加: 1:.NETFramework程序可以在引用右击添加引用,然后添加System.Configuration 2:引入Nu ...
- C# 读取app.config配置文件 节点键值,提示 "配置系统未能初始化" 错误的解决方案
新建C#项目,在app.config中添加了appSettings项,运行时出现"配置系统未能初始化"的错误,MSDN里写到,如果配置文件中包含 configSections 元素 ...
- 一个web.Config或app.Config自定义段configSections的示例--转
直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml配置文件,简洁方便得多.这两个配置文件不仅有常见的connectionStrings和appSetti ...
- winform中读取App.config中数据连接字符串
1.首先要在工程引用中导入System.Configuration.dll文件的引用. 2.通过System.Configuration.ConfigurationManager.Connection ...
随机推荐
- Java中JIN机制及System.loadLibrary() 的执行过程
Android平台Native开发与JNI机制详解 http://mysuperbaby.iteye.com/blog/915425 个人认为下面这篇转载的文章写的很清晰很不错. 注意Android平 ...
- 安卓工程修改包名后 Failed to find provider info for...问题
安卓工程修改包名后 Failed to find provider info for com.android...provider问题 原因: 1. 多处含包名文件须同时更改 2. Manifest里 ...
- reactjs 入门
地址搜集:http://www.cocoachina.com/webapp/20150604/12035.html class 属性需要写成 className ,for 属性需要写成 htmlFor ...
- 【SSMS增强工具】SQL Sharper 2014介绍
产品介绍 SQL Sharper是一款SQL Server Management Studio插件,用于数据库对象快速查询.表结构查询.优化查询结果导出.代码生成等方面. 适用人群:T-SQL开发者. ...
- [转帖]SD卡&FLASH&USB
来源:http://www.cypress.com Cypress官网,了解任何芯片都应该从它的官网入手,资料一定是最多最原始的,像Ronnie学习. Cypress’s EZ-USB® FX2LP™ ...
- oracle 报Ora-01008错误:oracle 并非所有变量都已绑定的原因.TO_number();动态执行select..into..语句时
1.sql_temp := 'UPDATE B38_back SET '||code||'=TO_NUMBER(nvl('||:NEW.BACAI||',0))+'||OnMonth || ' WHE ...
- 系统重装c盘后,mysql重新设置
之前我的mysql装在d盘,重装了系统后,虽然只格式化了c盘,但mysql还是不能用了.我网上找了找.修改了一下配置. 1.首先设置环境变量,编辑path,在后面添加上mysql的安装路径 : 2.之 ...
- group by应用
注意: having是对分组后的数据进行第二次筛选或者过滤,也就是说没有group by就没having where之后不能有聚合函数 查询每个年级的总学时数,并按照升序排列select GradeI ...
- SpringMVC4+thymeleaf3的一个简单实例(篇一:基本环境)
首语:用SpringMVC和thymeleaf实现一个简单的应用,包括基本环境搭建,SpringMVC4和thymeleaf3的整合,页面参数的获取,页面参数验证,以及用MySQL保存数据.我会把步骤 ...
- MySQL中删除重复数据只保留一条
用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 SELECT ...