一:程序截图

二:具体代码

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自定义标签的值的更多相关文章

  1. C# 读取app.config配置文件节点键值,提示"配置系统未能初始化" 错误的解决方案

    MSDN里写到, 如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素. 将自己添加的appSettin ...

  2. 【Core】.NET Core中读取App.config配置文件

    1.项目中添加App.config文件 因为.NET Core的项目本质是控制台应用,所以ConfigurationManager的API会去默认读取app.config配置文件,而不是web.con ...

  3. 一个web.Config或app.Config自定义段configSections的示例

    一个web.Config或app.Config自定义段configSections的示例 越来越觉得,直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml ...

  4. C# App.config 自定义 配置节 出现的问题:配置系统未能初始化

    C# 读取app.config配置文件 节点键值,提示 "配置系统未能初始化" 错误的解决方案 新建C#项目,在app.config中添加了appSettings项,运行时出现&q ...

  5. [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法

    本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...

  6. C#读取App.config/Web.config

    读取需要添加 System.Configuration 引用, 两种方式添加: 1:.NETFramework程序可以在引用右击添加引用,然后添加System.Configuration 2:引入Nu ...

  7. C# 读取app.config配置文件 节点键值,提示 "配置系统未能初始化" 错误的解决方案

    新建C#项目,在app.config中添加了appSettings项,运行时出现"配置系统未能初始化"的错误,MSDN里写到,如果配置文件中包含 configSections 元素 ...

  8. 一个web.Config或app.Config自定义段configSections的示例--转

    直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml配置文件,简洁方便得多.这两个配置文件不仅有常见的connectionStrings和appSetti ...

  9. winform中读取App.config中数据连接字符串

    1.首先要在工程引用中导入System.Configuration.dll文件的引用. 2.通过System.Configuration.ConfigurationManager.Connection ...

随机推荐

  1. 第八篇:python高级之多进程

    python高级之多进程   python高级之多进程 本节内容 多进程概念 Process类 进程间通讯 进程同步 进程池 1.多进程概念 multiprocessing is a package ...

  2. mssql 创建触发器

    MS-SMS里创建触发器: 首先右击表内的触发器文件夹图标 然后输入触发器创建指令,一下案例:(添加创建时间) 创建: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFI ...

  3. 10.30 afternoon

    P76竞赛时间: ????年??月??日??:??-??:?? 题目名称 他 她 它 名称 he she it 输入 he.in she.in it.in 输出 he.out she.out it.o ...

  4. PL/SQL Select into 异常处理

    在使用select into 为变量赋值时,如果变量是集合类型,不会产生异常,而如果是基本类型或记录类型,则会报异常. 异常产生了怎么办?当然是捕获并处理啦. 对于普通的代码块来说,在代码块的结尾处理 ...

  5. SQL server 使用触发器跨数据库备份数据

    create database TriggerTest create table transInfo2 --交易信息表 ( cardID ) not null, --卡号 transType ) no ...

  6. mac 下安装安卓模拟器

    mac下的安卓模拟器——bluestacks 下载:http://pan.baidu.com/s/1kVvZyYz 该安卓模拟器除了能网络下载apk,本地还能安装apk,但是与win版不同,安装不是那 ...

  7. android SDK 代理配置(东北大学)

    启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Settings』窗口: 在『Andro ...

  8. XFire中Services.xml 配置的一些细节

    昨天第一次调XFire引擎,放在Tomcat里之后老是报错,检查了很久没有发现什么问题,一直很费解. 后来在网上看到一篇文章<XFire services.xml 配置文件补充说明>,根绝 ...

  9. 关于ASP.NET控件方面的学习(恢复版)

    前段时间没有把学习中的遇到的问题和解决方法详细总结,今天整理整理.. 鉴于我们这个研究生论文管理系统是小组形式,所以说虽然我只负责数据库,但是其它部分也多少有些工作方面的涉及,最后感谢各位同学和老师的 ...

  10. index ffs、index fs原理考究-1109

    h2 { margin-top: 0.46cm; margin-bottom: 0.46cm; direction: ltr; line-height: 173%; text-align: justi ...