29

In my winform app, I am trying to add a userSetting, although the error is occuring with appSettings too. When the setting is added I get an exeption thrown that says: "Configuration system failed to initialize" with a Inner Exception "Unrecognized configuration section userSetting"

Exception Details:

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: 
AdamSane
  • 2,831
  • 1
  • 24
  • 28

12 Answers12

33

I had some user settings, then removed all of them and started seeing this exception - but only when running the app without debugging. It worked fine when debugging the app. The reason is that user-level settings are "cached" in the Local Application Data folder, and in fact are not read from the MYAPP.exe.config.
So what I did was go to C:\Users\MYUSERNAME\AppData\Local\MYCOMPANY\MYAPP.exe_Url_longnastyhash9982749827349879\1.0.0.0\user.config (this is on Win7, the path depends on OS) and removed that folder (with the long hash) altogether. The exception went away.
By the way, depending on how your settings are setup this user.config file may be under \AppData\Local or \AppData\Roaming.

AlexB
  • 7,302
  • 12
  • 56
  • 74
beluga
  • 826
  • 7
  • 14
  • 1
    The problem happened to me spontaneously. Nothing changed. A customer said it worked fine this morning, then he restarted the application and got this exception. – Mark Lakata Sep 12 '12 at 16:57
  • 1
    I fixed this by removing the offending userSettings section from that very config file, but removing the file altogether will probably work too. – Grimace of Despair Feb 07 '14 at 12:36
  • 1
    Similar to @MarkLakata, this spontaneously happened to our customer - working last night, broken today. The entire user.config file got filled with NULL characters. Thanks for the tip. – Smashery Oct 28 '15 at 05:33
  • 2
    It's amazing how many times a bug happens because of this AppData folder. They should fix it already. The problem is it's so counter-intuitive. – okkko Jan 08 '16 at 08:08
28

Try checking that the app.config (myapp.exe.config once deployed) file exists and has at the top (possibly with other bits)

<?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>
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    Thanks, but that's in there. Ive blown it away and recreated it a few times. – AdamSane Jan 14 '09 at 15:54
  • 14
    The important thing for me was "inside the "configuration" element, the first child must be the "configSections" element" (taken from http://stackoverflow.com/a/6472696/61697) – demoncodemonkey Dec 19 '11 at 17:42
  • 1
    Also i noticed that errors in app.config including invalid sectionGroup will cause this too – Beygi Jul 30 '12 at 17:46
5

I get this error when I write my App.Config (stupidly) as below.

<?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>

Notice, that there is no appSettings? I used to do this mistake regularly...

Kanini
  • 1,947
  • 6
  • 33
  • 58
4

I started seeing this message when I removed all userSettings. I was able to fix it by adding a single userSetting back into the settings files.

Fares
  • 669
  • 6
  • 11
2

I had a this problem today and found that I'd accidentally (not to mention erroneously) added a second custom configuration section to my App.config. Once I removed the errant addition, I was able to continue running my application without problems.

<?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>

Removing the second ABCConfig section resolved my problem. Hope this helps!

oddmeter
  • 754
  • 7
  • 16
  • 2
    This is true for any duplicated section. We were getting the "Configuration section failed to initialize" exception and I found that one of our developers had accidentally created two sections as a result of trying to remove a custom environment section group. – donperk May 12 '15 at 22:35
2

Clean Solution Delete all currently existing settings files as well as app.config Close VS Manually go in and clear out the bin folder and the obj folder of your project Restart PC Re-add the "Application Configuration File"

bulltorious
  • 7,769
  • 4
  • 49
  • 78
2

I had some garbage in my machine.config that was causing this error. Look for the exception stack trace and see if you have the same problem. It was essentially malformed XML.

ashes999
  • 9,925
  • 16
  • 73
  • 124
1

Probably the problem is that your config file doesn't comply with its schema. For example, this issue can be recreated by duplication the ConnectionStrings section.

RA.
  • 1,405
  • 1
  • 11
  • 18
0

I had this problem because I changed one application setting scope (from 'application' to user'). As I couldn't find a solution to resolve my problem, I decided to delete the settings file into the solution explorer. After that, I opened the properties, in the tab 'settings', I clicked where it was proposed to create the settings file. And a new settings file has been created with the values I defined into the previous settings file. I rebuilt my project and it worked well.

jonsca
  • 10,218
  • 26
  • 54
  • 62
Dom
  • 1
0

The startup tag did the trick for me on VS Pro 2013 version 12.021005.1 with .Net 4.5.51650 for console application (i.e. app.config file)

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

Just posting here as I found my fix to the issue but don't see it listed here.... I added an appSettings tag under the main configuration node and when I ran it I got the same error as OP. What fixed it was making sure the configSections node preceeded my appSettings node. enter image description here

MarkyMarksFunkyBunch
  • 1,060
  • 10
  • 10
0

I got this error when a .NET 4.x build of my app has already generated the common user.config file under the AppData folder, and then I launched the .NET 3.5 version.

And though deleting the user.config under %APPDATA%\Local\<Company>\<AppName>_StrongName_*\<Version> as per @beluga's suggestion solves the issue, the settings will also be lost, and it can be quite frustrating for a user to dig up the file to delete if this happens.

But actually there is a way even for a .NET 3.5 build to be able to use the config file.

The culprit is the sectionGroup element where the UserSettingsGroup type uses the .NET 4.x assembly identity:

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

To handle this from the app you need to apply some custom assembly resolution logic:

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

where the event handler:

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;
} 
György Kőszeg
  • 17,093
  • 6
  • 37
  • 65