进入公司一年多来,对配置文件添加了不少参数,但是从未想过这些参数是如何被读取出来的,今天把读取参数的处理看了一下,收获不少。假定有App.config如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="family" type="FirstRowTest.Configuration.Settings,FirstRowTest"/>
</configSections>
<family>
<familymember>
<member status="father"
birth="1954"
name="AA"
hobby="reading books">
</member>
<member status="mother"
birth="1958"
name="BB"
hobby="chatting">
</member>
<member status="elder brother"
birth="1982"
name="CC"
hobby="money">
</member>
<member status="younger brother"
birth="1986"
name="DD"
hobby="computer">
</member>
</familymember>
</family>
<appSettings>
<add key="address" value="maling"/>
</appSettings>
</configuration>
构造:
public class Param:System.Configuration.ConfigurationElement
{
[System.Configuration.ConfigurationProperty("status")]
public string Status
{
get
{
return this["status"] as string;
}
set
{
this["status"] = value;
}
}
[System.Configuration.ConfigurationProperty("birth")]
public string Birth
{
get
{
return this["birth"] as string;
}
set
{
this["birth"] = value;
}
}
[System.Configuration.ConfigurationProperty("name")]
public string Name
{
get
{
return this["name"] as string;
}
set
{
this["name"] = value;
}
}
[System.Configuration.ConfigurationProperty("hobby")]
public string Hobby
{
get
{
return this["hobby"] as string;
}
set
{
this["hobby"] = value;
}
}
}
public class Params:System.Configuration.ConfigurationElementCollection
{
protected override System.Configuration.ConfigurationElement CreateNewElement()
{
return new Param();
}
protected override object GetElementKey(System.Configuration.ConfigurationElement element)
{
Param param = element as Param;
return param.Status;
}
protected override string ElementName
{
get
{
return "member";
}
}
public override System.Configuration.ConfigurationElementCollectionType CollectionType
{
get
{
return System.Configuration.ConfigurationElementCollectionType.BasicMap;
}
}
}
public class Settings:System.Configuration.ConfigurationSection
{
[System.Configuration.ConfigurationProperty("familymember")]
public Params FamilyMember
{
get
{
return this["familymember"] as Params;
}
set
{
this["familymember"] = value;
}
}
}
调用:
public FrmMain()
{
InitializeComponent();
}
private void FrmMain_Load(object sender, EventArgs e)
{
System.Configuration.Configuration cfg = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
//Settings setting = cfg.GetSection("family") as Settings;
Settings setting = System.Configuration.ConfigurationManager.GetSection("family") as Settings;
Param first = null;
if (null != setting)
{
foreach (Param p in setting.FamilyMember)
{
if (null == first)
{
first = p;
}
this.richTextBox1.AppendText(string.Format("status:{0}|birth:{1}|name:{2}|hobby:{3}\r\n",
p.Status, p.Birth, p.Name, p.Hobby));
}
}
//cfg.Save();
System.Configuration.ConfigurationManager.RefreshSection("family");
}
}
如果需要进行保存,则处理上有一点变动:
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void FrmMain_Load(object sender, EventArgs e)
{
System.Configuration.Configuration cfg = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
Settings setting = cfg.GetSection("family") as Settings;
//Settings setting = System.Configuration.ConfigurationManager.GetSection("family") as Settings;
Param first = null;
if (null != setting)
{
foreach (Param p in setting.FamilyMember)
{
if (null == first)
{
first = p;
}
this.richTextBox1.AppendText(string.Format("status:{0}|birth:{1}|name:{2}|hobby:{3}\r\n",
p.Status, p.Birth, p.Name, p.Hobby));
}
}
first.Name = "I dont know";
cfg.Save();
//System.Configuration.ConfigurationManager.RefreshSection("family");
}
保存结果:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="family" type="FirstRowTest.Configuration.Settings,FirstRowTest"/>
</configSections>
<family>
<familymember>
<member status="father" birth="1954" name="I dont know" hobby="reading books" />
<member status="mother" birth="1958" name="BB" hobby="chatting" />
<member status="elder brother" birth="1982" name="CC" hobby="money" />
<member status="younger brother" birth="1986" name="DD" hobby="computer" />
</familymember>
</family>
<appSettings>
<add key="address" value="maling"/>
</appSettings>
</configuration>
上面学习了配置文件读取的处理方式,但是没有对经常用到的 <appSettings>进行学习,其实这些参数的读取要简单得多:
假设有如下配置参数
<appSettings>
<add key="address" value="China"/>
</appSettings>
读取:
string address=System.Configuration.ConfigurationManager.AppSettings["address"].ToString();
事实就是这么简单