首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用c# 4.0 app.config中的部分?

如何使用c# 4.0 app.config中的部分?
EN

Stack Overflow用户
提问于 2011-01-12 23:40:24
回答 2查看 80.8K关注 0票数 62

我想使用我的应用程序配置来存储两个公司的设置,我更喜欢使用一个分区将一个公司的数据与另一个公司的数据分开,而不是给它们不同的键名。

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

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

代码语言:javascript
复制
  <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:

代码语言:javascript
复制
<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>

代码:

代码语言:javascript
复制
// 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";
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-12 23:44:32

代码语言:javascript
复制
<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>

然后:

代码语言:javascript
复制
var section = ConfigurationManager.GetSection("FSCS") as NameValueCollection;
var value = section["processingDirectory"];
票数 87
EN

Stack Overflow用户

发布于 2014-05-24 02:59:03

App.config

代码语言:javascript
复制
<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>

示例用法

代码语言:javascript
复制
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"]
}
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4670669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档