如何在 app.config 中创建自定义配置部分
我想在 app.config 文件中添加自定义配置部分,如下所示
<Companies>
<Company name="" code=""/>
<Company name="" code=""/>
</Companies>
<Employees>
<Employee name="" Des="" addr="" sal=""/>
<Employee name="" Des="" addr="" sal=""/>
</Employeess>
<Departments>
<Department Id="" Projects=""/>
</Departments>
<Projects>
<Project Path=""/>
</Projects>
在“部门”部分中,它指的是“项目”部分。
有人能告诉我怎么做吗?以及如何在我的代码中访问它?
@Bhaskar:请找到您的评论的代码。
public class RegisterCompaniesConfig : ConfigurationSection
{
public static RegisterCompaniesConfig GetConfig()
{
return (RegisterCompaniesConfig)System.Configuration.ConfigurationManager.GetSection("RegisterCompanies")?? new RegisterCompaniesConfig();
}
[System.Configuration.ConfigurationProperty("Companies")]
public Companies Companies
{
get
{
object o = this["Companies"]; return o as Companies;
}
}
}
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);
}
}
protected override System.Configuration.ConfigurationElement CreateNewElement()
{ return new Company();
}
protected override object GetElementKey(System.Configuration.ConfigurationElement element)
{ return ((Company)element).Name; }
}
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; } }
}
I want to add the custom configsection in the app.config file as follows
<Companies>
<Company name="" code=""/>
<Company name="" code=""/>
</Companies>
<Employees>
<Employee name="" Des="" addr="" sal=""/>
<Employee name="" Des="" addr="" sal=""/>
</Employeess>
<Departments>
<Department Id="" Projects=""/>
</Departments>
<Projects>
<Project Path=""/>
</Projects>
In the Department section it is referring to Projects section.
Can anybody tell me way to do it? And how to access it in my code?
@Bhaskar: Please find the code for your comment.
public class RegisterCompaniesConfig : ConfigurationSection
{
public static RegisterCompaniesConfig GetConfig()
{
return (RegisterCompaniesConfig)System.Configuration.ConfigurationManager.GetSection("RegisterCompanies")?? new RegisterCompaniesConfig();
}
[System.Configuration.ConfigurationProperty("Companies")]
public Companies Companies
{
get
{
object o = this["Companies"]; return o as Companies;
}
}
}
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);
}
}
protected override System.Configuration.ConfigurationElement CreateNewElement()
{ return new Company();
}
protected override object GetElementKey(System.Configuration.ConfigurationElement element)
{ return ((Company)element).Name; }
}
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; } }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该查看 CodeProject 上 Jon Rista 关于 .NET 2.0 配置的三部分系列。
强烈推荐,写得很好,非常有帮助!我从这些优秀的文章中学会了如何处理自定义配置部分。
You should check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.
Highly recommended, well written and extremely helpful! I've learned how to deal with custom config sections from those excellent articles.
我的所有配置管理代码都基于我在此处收集的类。这是一个示例,这里有一些文档。请注意,这是我亲自从博客文章中重构的代码,该代码不再在线可用。
I base all my configuration management code on the classes I collected here. This is an example, and here's some documentation. Note that this is code I personally refactored from a blog post that isn't available on-line any more.
这会帮助你
http://msdn.microsoft.com/en-我们/library/2tw134k3(v=vs.80).aspx
This will help you
http://msdn.microsoft.com/en-us/library/2tw134k3(v=vs.80).aspx