当我尝试使用以下命令检索.config文件中的节列表时
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);config.Sections集合包含一堆系统部分,但我在configSections标记中定义的文件中没有任何部分。
发布于 2013-03-07 02:09:11
这是一个blog article,它可以让你得到你想要的东西。但为了确保答案保持可用,我也将在此处删除代码。简而言之,确保引用的是System.Configuration程序集,然后利用ConfigurationManager类获取所需的非常特定的部分。
using System;
using System.Configuration;
public class BlogSettings : ConfigurationSection
{
private static BlogSettings settings
= ConfigurationManager.GetSection("BlogSettings") as BlogSettings;
public static BlogSettings Settings
{
get
{
return settings;
}
}
[ConfigurationProperty("frontPagePostCount"
, DefaultValue = 20
, IsRequired = false)]
[IntegerValidator(MinValue = 1
, MaxValue = 100)]
public int FrontPagePostCount
{
get { return (int)this["frontPagePostCount"]; }
set { this["frontPagePostCount"] = value; }
}
[ConfigurationProperty("title"
, IsRequired=true)]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;’\"|\\"
, MinLength=1
, MaxLength=256)]
public string Title
{
get { return (string)this["title"]; }
set { this["title"] = value; }
}
}请确保您阅读了博客文章-它将为您提供背景知识,以便您可以将其应用到您的解决方案中。
https://stackoverflow.com/questions/15254566
复制相似问题