.NET 3.5 - 配置系统初始化异常失败

29

在我的WinForm应用程序中,我试图添加一个用户设置,但是在使用appSettings时也出现了错误。当添加设置时,我收到一个抛出的异常,该异常说:“配置系统初始化失败”,并带有一个内部异常“未识别的配置部分userSetting”。

异常详细信息:

System.Configuration.ConfigurationErrorsException was unhandled
  Message="Configuration system failed to initialize"
  Source="System.Configuration"
  BareMessage="Configuration system failed to initialize"
  Line=0
  StackTrace:
       at System.Configuration.ConfigurationManager.PrepareConfigSystem()
       at System.Configuration.ConfigurationManager.RefreshSection(String sectionName)
       at System.Configuration.ClientSettingsStore.ReadSettings(String sectionName, Boolean isUserScoped)
       at System.Configuration.LocalFileSettingsProvider.GetPropertyValues(SettingsContext context, SettingsPropertyCollection properties)
       at System.Configuration.SettingsBase.GetPropertiesFromProvider(SettingsProvider provider)
       at System.Configuration.SettingsBase.GetPropertyValueByName(String propertyName)
       at System.Configuration.SettingsBase.get_Item(String propertyName)
       at System.Configuration.ApplicationSettingsBase.GetPropertyValue(String propertyName)
       at System.Configuration.ApplicationSettingsBase.get_Item(String propertyName)
       at Settings.get_ApplicationData() in \Properties\Settings.Designer.cs:line 41
       at Common.Initialize.IsSettingsInitialized() 
       at SurveyClient.Program.Main() 
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Configuration.ConfigurationErrorsException
       Message="Unrecognized configuration section userSettings.
       Source="System.Configuration"
       BareMessage="Unrecognized configuration section userSettings."
       Line=3
       StackTrace:
            at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
            at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
            at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
            at System.Configuration.ClientConfigurationSystem.OnConfigRemoved(Object sender, InternalConfigEventArgs e)
       InnerException: 

非常明显...“Unrecognized configuration section userSettings.”(无法识别的配置部分userSettings)。 - The Muffin Man
12个回答

33
我之前有一些用户设置,然后将它们全部删除了,结果开始出现这个异常 - 但只有在没有调试运行应用程序时才会出现。在调试应用程序时,它正常工作。原因是用户级别的设置被“缓存”在本地应用程序数据文件夹中,并且实际上并不是从MYAPP.exe.config中读取的。
所以我做的是进入C:\Users\MYUSERNAME\AppData\Local\MYCOMPANY\MYAPP.exe_Url_longnastyhash9982749827349879\1.0.0.0\user.config(这是在Win7上,路径取决于操作系统),然后将那个带有长哈希值的文件夹完全删除。异常问题就解决了。
顺便说一下,根据您的设置方式,这个user.config文件可能位于\AppData\Local\AppData\Roaming下。

1
问题突然发生了。没有什么改变。一个客户说今天早上它运行正常,然后他重新启动应用程序就出现了这个异常。 - Mark Lakata
1
我通过从配置文件中删除有问题的userSettings部分来解决了这个问题,但是完全删除该文件也可能起作用。 - Grimace of Despair
1
与@MarkLakata类似,这也发生在我们的客户身上 - 昨晚还能工作,今天就坏了。整个user.config文件都被NULL字符填满了。感谢您的提示。 - Smashery
2
真是令人惊讶,由于AppData文件夹的存在,错误居然发生了这么多次。他们应该早点修复它。问题在于它太不符合直觉了。 - okkko

28

尝试检查应用程序配置文件 (在部署后为 myapp.exe.config) 是否存在,并且是否在顶部包含了以下内容(可能还有其他内容)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings"
    type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>

1
谢谢,但那已经在里面了。我已经删除并重新创建了几次。 - AdamSane
14
对我而言,重要的是“在‘配置’元素内,第一个子元素必须是‘configSections’元素”(摘自https://dev59.com/p2w15IYBdhLWcg3wu-E9#6472696)。 - demoncodemonkey
1
我还注意到,app.config中的错误,包括无效的sectionGroup,也会导致这种情况发生。 - Beygi

5

当我愚蠢地写下如下的App.Config时,就会出现这个错误。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<!--List of XMLFiles Begins -->
    <add key="ConfigFileEnvironment" value="C:\Program Files\MyProduct\Config\Environment.xml" />
<!--List of XMLFiles Ends -->

</configuration>

请注意,这里没有appSettings?我过去经常犯这个错误...

4

当我删除了所有的用户设置时,我开始看到这个消息。通过将单个用户设置添加回设置文件中,我成功解决了这个问题。


2
今天我遇到了一个问题,发现我无意中(更不用说错误地)在我的App.config文件中添加了第二个自定义配置部分。一旦我删除了这个错误的部分,我就能够继续运行我的应用程序而没有出现问题。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="ABCConfig" type="ABC.Configuration.ABCConfigurationSection, ABC"/>
    <!-- Other custom section definitions -->
  </configSections>
  <connectionStrings>
      <!-- Connection strings go here -->
  </connectionStrings>

  <!-- Configure ABC -->
  <ABCConfig CustomA="blah" CustomB="stuff" />

  <!-- Other custom sections -->

  <!-- Errant addition to Configure ABC which causes the problem - SHOULD NOT BE HERE -->
  <ABCConfig CustomA="blah" CustomB="stuff" />

</configuration>

删除第二个ABCConfig部分解决了我的问题。希望这可以帮到你!


2
这适用于任何重复的部分。我们遇到了“配置节初始化失败”的异常,我发现我们的一位开发人员在尝试移除自定义环境节组时意外创建了两个<connectionString>部分。 - donperk

2

清理解决方案 删除所有当前存在的设置文件以及app.config 关闭VS 手动清除项目的bin文件夹和obj文件夹中的内容 重新启动电脑 重新添加“应用程序配置文件”


2

我在我的machine.config文件中发现了一些垃圾代码,导致出现了这个错误。查看异常堆栈跟踪,看看是否有同样的问题。它基本上是格式不正确的XML。


1

可能的问题是您的配置文件不符合其模式。例如,通过复制ConnectionStrings部分可以重新创建此问题。


0

在我的VS Pro 2013版本12.021005.1上,使用启动标签对于控制台应用程序(即app.config文件)的.Net 4.5.51650非常有效。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
 <connectionStrings>
 ...
</connectionStrings>
</configuration>

0

当我的应用程序的.NET 4.x版本已经在AppData文件夹下生成了常见的user.config文件,然后我启动了.NET 3.5版本时,我遇到了这个错误。

虽然按照@beluga的建议删除%APPDATA%\Local\<Company>\<AppName>_StrongName_*\<Version>下的user.config可以解决问题,但是设置也会丢失,如果发生这种情况,用户挖掘文件进行删除可能会非常令人沮丧。

但实际上,即使是.NET 3.5版本,也有一种方法可以使用配置文件。

罪魁祸首是sectionGroup元素,其中UserSettingsGroup类型使用了.NET 4.x程序集标识:

<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">

要从应用程序处理这个问题,您需要应用一些自定义的程序集解析逻辑:

// somewhere in your application startup:
AppDomain.CurrentDomain.AssemblyResolve += HandleAssemblyResolve;

事件处理程序所在的位置:

private static Assembly HandleAssemblyResolve(object sender, ResolveEventArgs args)
{
    // System.dll, any version
    if (args.Name.StartsWith("System, Version=", StringComparison.Ordinal))
        return typeof(UserSettingsGroup).Assembly;

    // for any other assembly letting the default logic take over
    return null;
} 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接