配置系统无法初始化==>无法识别的配置部分

时间:2014-10-10 16:28:45

标签: c# winforms visual-studio

我甚至不知道我是否可以做我正在尝试的事情,但我从几个项目中导入了表单并添加了对这些项目的引用。每个项目都有一组不同的连接字符串,我试图让它们在App.config中共存,我可以通过SECTION过滤(用户从组合框中选择连接)。我希望我可以通过实现ConfigSections来实现这一点。如果它可行,我显然不知道如何。

附件是我的App.config。我收到错误'配置系统无法初始化',当我深入细节时,它说'无法识别的配置部分amSettings

我正在尝试做什么?如果是这样,我需要纠正什么?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="ApplicationSettings" 
                      type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="amSettings.Properties.Settings" 
                     type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
                     allowExeDefinition="MachineToLocalUser" 
                     requirePermission="false"/>
            <section name="cbSettings.Properties.Settings"
         type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
         requirePermission="false"/>
        </sectionGroup>
    </configSections>

    <amSettings>
        <add key="VX130 Attribute Map Connections" value="Sample Console Application" />
        <add key="Region 1 VX130"   value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 2 VX130"   value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 3 VX130"   value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 4 VX130"   value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="CDW"  value="Server=VHACDWA01;Database=;Trusted_Connection=true;"/>
    </amSettings>
    <cbSettings>
        <add key="CDW Class Builder Connections" value="Sample Console Application" />
        <add key="Region 1 Class Build"     value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 2 Class Build"     value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 3 Class Build"     value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 4 Class Build"     value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="DEVELOPMENT Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
        <add key="PREVIEW Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
        <add key="VERSION Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
    </cbSettings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

2 个答案:

答案 0 :(得分:0)

将部分名称从amSettings.Properties.Settings更改为amSettings并将cbSettings.Properties.Settings更改为cbSettings

e.g。

    `<section name="amSettings" `

以下是一个全面的例子:

如果您将配置文件更改为:

    <configSections>
        <section name="amSettings"
                 type="System.Configuration.AppSettingsSection"
                 allowExeDefinition="MachineToLocalUser"
                 requirePermission="false"/>
        <section name="cbSettings"
     type="System.Configuration.AppSettingsSection"
     requirePermission="false"/>
  </configSections>
  <amSettings>
      <add key="ABC" value="DEF"/>
  </amSettings>

然后您可以使用以下代码访问密钥ABC:

        var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection("amSettings");
        var a = appSettingSection.Settings["ABC"].Value;

答案 1 :(得分:0)

解决方案是两件事。将部分名称更改为user469104推荐并将部分包装在组名称中。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="ApplicationSettings" 
                      type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="amSettings" 
                     type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
                     allowExeDefinition="MachineToLocalUser" 
                     requirePermission="false"/>
            <section name="cbSettings"
         type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
         requirePermission="false"/>
        </sectionGroup>
    </configSections>
<ApplicationSettings>
    <amSettings>
        <add key="VX130 Attribute Map Connections" value="Sample Console Application" />
        <add key="Region 1 VX130"   value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 2 VX130"   value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 3 VX130"   value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 4 VX130"   value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="CDW"  value="Server=VHACDWA01;Database=;Trusted_Connection=true;"/>
    </amSettings>
    <cbSettings>
        <add key="CDW Class Builder Connections" value="Sample Console Application" />
        <add key="Region 1 Class Build"     value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 2 Class Build"     value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 3 Class Build"     value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 4 Class Build"     value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="DEVELOPMENT Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
        <add key="PREVIEW Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
        <add key="VERSION Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
    </cbSettings>
</ApplicationSettings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>