读取App.config自定义标签的值

时间:2022-02-09 13:01:09

一:程序截图

读取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包含到项目即可