本文介绍了如何在 app.config 中创建自定义配置部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的 app.config
文件中添加自定义配置部分.有没有办法做到这一点,以及如何在我的程序中访问这些设置.以下是我想添加到我的 app.config
的配置部分:
<公司><公司名称=塔塔汽车"代码=塔塔"/><公司名称=本田汽车"代码=本田"/></公司></注册公司>
解决方案
导入命名空间:
使用 System.Configuration;
创建 ConfigurationElement 公司:
公共类公司:ConfigurationElement{[ConfigurationProperty("name", IsRequired = true)]公共字符串名称{得到{将 this["name"] 作为字符串返回;}}[ConfigurationProperty("code", IsRequired = true)]公共字符串代码{得到{将 this["code"] 作为字符串返回;}}}
配置元素集合:
公共类公司: 配置元素集合{上市公司 this[int index]{得到{返回 base.BaseGet(index) 作为 Company ;}放{if (base.BaseGet(index) != null){base.BaseRemoveAt(index);}this.BaseAdd(index, value);}}public new Company this[string responseString]{得到{返回(公司)BaseGet(响应字符串);}放{if(BaseGet(responseString) != null){BaseRemoveAt(BaseIndexOf(BaseGet(responseString)));}基础添加(值);}}受保护的覆盖 System.Configuration.ConfigurationElement CreateNewElement(){返回新公司();}受保护的覆盖对象 GetElementKey(System.Configuration.ConfigurationElement 元素){return ((Company)element).Name;}}
和配置部分:
公共类 RegisterCompaniesConfig: 配置部分{公共静态 RegisterCompaniesConfig GetConfig(){返回 (RegisterCompaniesConfig)System.Configuration.ConfigurationManager.GetSection("RegisterCompanies") ??新注册公司配置();}[System.Configuration.ConfigurationProperty("公司")][ConfigurationCollection(typeof(Companies), AddItemName = "Company")]上市公司 公司{得到{对象 o = this["公司"];返回 o 作为公司;}}}
并且您还必须在 web.config (app.config) 中注册您的新配置部分:
<configSections><section name="Companies" type="blablabla.RegisterCompaniesConfig" ..>然后你加载你的配置
var config = RegisterCompaniesConfig.GetConfig();foreach(config.Companys 中的 var 项目){做点什么 ..}
I want to add a custom configuration section in my app.config
file.Is there a way to do it and how can I access these settings in my program.Following is the config section I want to add to my app.config
:
<RegisterCompanies>
<Companies>
<Company name="Tata Motors" code="Tata"/>
<Company name="Honda Motors" code="Honda"/>
</Companies>
</RegisterCompanies>
解决方案
Import namespace :
using System.Configuration;
Create ConfigurationElement Company :
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;
}
}
and 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 ;
}
}
}
and you must also register your new configuration section in web.config (app.config):
<configuration>
<configSections>
<section name="Companies" type="blablabla.RegisterCompaniesConfig" ..>
then you load your config with
var config = RegisterCompaniesConfig.GetConfig();
foreach(var item in config.Companies)
{
do something ..
}
这篇关于如何在 app.config 中创建自定义配置部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!