亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在app.config中創建自定義配置部分?

如何在app.config中創建自定義配置部分?

C#
Cats萌萌 2019-11-27 09:52:00
我想在app.config文件中添加一個自定義配置部分。有沒有辦法做到這一點,以及如何在程序中訪問這些設置。以下是我要添加到我的配置部分app.config:<RegisterCompanies>    <Companies>      <Company name="Tata Motors" code="Tata"/>      <Company name="Honda Motors" code="Honda"/>    </Companies></RegisterCompanies>
查看完整描述

3 回答

?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

導入名稱空間:


using System.Configuration;

創建ConfigurationElement公司:


public class Company : ConfigurationElement

{


        [ConfigurationProperty("name", IsRequired = true)]

        public string Name

        {

            get

            {

                return this["name"] as string;

            }

        }

            [ConfigurationProperty("code", IsRequired = true)]

        public string Code

        {

            get

            {

                return this["code"] as string;

            }

        }

}

ConfigurationElementCollection:


public class Companies

        : ConfigurationElementCollection

    {

        public Company this[int index]

        {

            get

            {

                return base.BaseGet(index) as Company ;

            }

            set

            {

                if (base.BaseGet(index) != null)

                {

                    base.BaseRemoveAt(index);

                }

                this.BaseAdd(index, value);

            }

        }


       public new Company this[string responseString]

       {

            get { return (Company) BaseGet(responseString); }

            set

            {

                if(BaseGet(responseString) != null)

                {

                    BaseRemoveAt(BaseIndexOf(BaseGet(responseString)));

                }

                BaseAdd(value);

            }

        }


        protected override System.Configuration.ConfigurationElement CreateNewElement()

        {

            return new Company();

        }


        protected override object GetElementKey(System.Configuration.ConfigurationElement element)

        {

            return ((Company)element).Name;

        }

    }

和ConfigurationSection:


public class RegisterCompaniesConfig

        : ConfigurationSection

    {


        public static RegisterCompaniesConfig GetConfig()

        {

            return (RegisterCompaniesConfig)System.Configuration.ConfigurationManager.GetSection("RegisterCompanies") ?? new RegisterCompaniesConfig();

        }


        [System.Configuration.ConfigurationProperty("Companies")]

            [ConfigurationCollection(typeof(Companies), AddItemName = "Company")]

        public Companies Companies

        {

            get

            {

                object o = this["Companies"];

                return o as Companies ;

            }

        }


    }

并且您還必須在web.config(app.config)中注冊新的配置部分:


<configuration>       

    <configSections>

          <section name="Companies" type="blablabla.RegisterCompaniesConfig" ..>

然后你用


var config = RegisterCompaniesConfig.GetConfig();

foreach(var item in config.Companies)

{

   do something ..

}


查看完整回答
反對 回復 2019-11-27
?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

您應該在CodeProject上查看Jon Rista的有關.NET 2.0配置的三部分系列。


揭示.NET 2.0配置的奧秘

解碼.NET 2.0配置的奧秘

揭開.NET 2.0配置之謎

強烈推薦,寫得很好,非常有幫助!


它非常清楚地向您展示了如何編寫必要的類(來自ConfigurationElement和/或ConfigurationSection),以設計所需的自定義配置部分。


查看完整回答
反對 回復 2019-11-27
?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

得注意的是,如果您使用的是MVC應用程序,則列出的部分很好。隨著一個控制臺應用程序,Web服務,也許還有其他,你需要有“的AssemblyName”‘blablabla.RegisterCompaniesConfig’后 

查看完整回答
反對 回復 2019-11-27
  • 3 回答
  • 0 關注
  • 584 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號