【问题标题】:How do you use sections in c# 4.0 app.config?您如何使用 c# 4.0 app.config 中的部分?
【发布时间】:2011-06-07 22:13:40
【问题描述】:

我想使用我的应用程序配置来存储 2 家公司的设置,如果可以使用一个部分将数据与另一个分开,而不是给他们不同的键名,我会更喜欢。

我一直在网上查看,但当人们使用部分或找到过时的简单使用方法时,我似乎有点不知所措。谁能给我一份关于它们的初学者指南?

下面是我的 app.config 的示例:

  <configSections>
    <section name="FBI" type="" />
    <section name="FSCS" type="" />
  </configSections>

  <FSCS>
    <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
  </FSCS>
  <FBI>
    <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
  </FBI>

更新:

基于答案的高级解决方案。以防有人想知道。

App.config:

<configuration>
    <configSections>
        <sectionGroup name="FileCheckerConfigGroup">
          <section name="FBI" type="System.Configuration.NameValueSectionHandler" />
          <section name="FSCS" type="System.Configuration.NameValueSectionHandler" />
        </sectionGroup>
    </configSections>
    <FileCheckerConfigGroup>
        <FSCS>
            <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
        </FSCS>
        <FBI>
            <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
        </FBI>
    </FileCheckerConfigGroup>
</configuration>

代码:

// Get the application configuration file. 
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Get the collection of the section groups. 
ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;

foreach (ConfigurationSectionGroup sectionGroup in sectionGroups)
{
    if (sectionGroup.Name == "FileCheckerConfigGroup")
    {
        foreach (ConfigurationSection configurationSection in sectionGroup.Sections)
        {
          var section = ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName) as NameValueCollection;
          inputDirectory = section["inputDirectory"]; //"C:\\testfiles";
        }
    }
}

【问题讨论】:

  • 您如何知道要使用哪家公司的数据?那你怎么知道你是 FBI 的用户?
  • 设置好输入目录后,会有一个方法为那个公司做事。

标签: c# .net configuration app-config configurationsection


【解决方案1】:
<configSections>
  <section name="FBI" type="System.Configuration.NameValueSectionHandler" />
  <section name="FSCS" type="System.Configuration.NameValueSectionHandler" />
</configSections>

<FSCS>
  <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
</FSCS>
<FBI>
  <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
</FBI>

然后:

var section = ConfigurationManager.GetSection("FSCS") as NameValueCollection;
var value = section["processingDirectory"];

【讨论】:

  • 这很棒。你会碰巧知道一种从代码中检索应用配置中所有不同部分的方法吗?
  • 注意:如果您使用的是 sectionGroups,则该组将作为路径添加到 get 部分,即 ConfigurationManager.GetSection("GroupName/FSCS") as NameValueCollection;
  • 在.net 2.0 以上的版本中你必须使用type="System.Configuration.AppSettingsSection"
【解决方案2】:

App.config

<configSections>
  <sectionGroup name="FileCheckers">
    <section name="FBI" type="System.Configuration.NameValueSectionHandler" />
    <section name="FSCS" type="System.Configuration.NameValueSectionHandler" />
  </sectionGroup>
</configSections>

<FileCheckers>
  <FSCS>
    <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
  </FSCS>
  <FBI>
    <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
  </FBI>
</FileCheckers>

示例用法

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionGroup fileCheckersGroup = config.SectionGroups["FileCheckers"];
foreach (ConfigurationSection section in fileCheckersGroup.Sections)
{
    NameValueCollection sectionSettings = ConfigurationManager.GetSection(section.SectionInformation.SectionName) as NameValueCollection;
    var value = sectionSettings["processingDirectory"]
}

【讨论】:

  • 请在代码之外提供一些解释。
  • +1 表示不只使用“var”定义。它有助于了解它的工作原理或您使用的类型。
  • 对于未来的网络搜索者,我通过子类化创建了一个自定义 ConfigSectionGroup——这似乎是错误的,因为此代码试图从 .NET 配置命名空间加载类,这导致了 TypeLoadException。进一步检查代码后,似乎没有必要。
  • 如果您收到 TypeInitializationException,请确保 configSections 元素是配置元素的第一个子元素。也应该只有 1 个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 1970-01-01
  • 2012-10-28
  • 1970-01-01
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多