C# App.config学习
進入公司一年多來,對配置文件添加了不少參數,但是從未想過這些參數是如何被讀取出來的,今天把讀取參數的處理看了一下,收獲不少。假定有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();
事實就是這么簡單
總結
以上是生活随笔為你收集整理的C# App.config学习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 理财过程如何避免跳坑?千万要注意这3点!
- 下一篇: C# 有什么惊艳到你的地方?