配置系统未能初始化===>;无法识别的配置节
本文关键字:配置 识别 系统 初始化 gt | 更新日期: 2023-09-27 18:26:08
我甚至不知道我是否能做我正在尝试的事情,但我已经从几个项目导入了表单,并添加了对这些项目的引用。每个项目都有一组不同的连接字符串,我正试图让它们在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>
将节名从amSettings.Properties.Settings更改为amSettings,将cbSettings.Properties.Settingss更改为cbSettings
例如
`<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;
解决方案有两件事。将节名称更改为user469104 recommended,并将节包装在Group name中。
<?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>