Dot-Net

.NET 3.5 - 配置系統未能初始化異常

  • January 14, 2009

在我的 winform 應用程序中,我正在嘗試添加一個 userSetting,儘管 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: 

嘗試檢查 app.config(部署後的 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>

我有一些使用者設置,然後刪除了所有設置並開始看到這個異常 - 但只有在執行應用程序而不調試時。調試應用程序時效果很好。原因是使用者級設置“記憶體”在本地應用程序數據文件夾中,實際上不是從 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 下。

引用自:https://stackoverflow.com/questions/443235