日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# App.config学习

發布時間:2023/12/10 C# 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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学习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。