Though I have not seen the error “Unrecognized configuration section userSetting” as a standalone exception, it has come up a number of times for me and surfaces as the inner exception on a “Configuration system failed to initialize” exception.
If you’re running into this error and following along, I’m currently using Visual Studio 2010, mostly on .NET 4.0 and I program in c#. If you’re using a different environment some of the file names or UI might be a bit different, so you’ll have to adjust accordingly.
The exception will be raised in Settings.Designer.cs, on the first attempt to read a value from the application’s configuration.
You can fix the error by navigating to the current directory where the user settings are being saved for your app. This will be in %system%\users\your_user\AppData\Local\Microsoft\YourApp.vshost.######\version
Delete the user.config file, clean your solution and rebuild all. This should fix the problem.
If that doesn’t work, you may need to go so far as to shut down Visual Studio, clean out all the %system%\users\your_user\AppData\Local\Microsoft\YourApp directories of any config files and then follow the above steps.
What’s Happening?
When you first add a setting to the application through the UI, the default is to create a user-scoped setting.
User settings and Application settings are primarily different on two fronts:
- Storage location
- Run-time accessibility
User settings are read/write and can be altered at run-time. These are perfect to save things like favorites, window layout, user-specific options, color preferences and the like. These files are stored in a directory created for the application, basically sand-boxed from system-wide access and the only place your app can read/write from for free (elevated privileges aside).
Application settings are read only at run-time and can only be altered by changing the XML file and relaunching the application, or by some creative XML file management (and then a reload on your configuration data). These files are stored in the app directory in app.config and also contain the default settings for users who have not yet launched the app (these are the settings that will be defaulted in for the new users).
I have hit the “Configuration system failed to initialize”Configuration system failed to initialize error a couple of times, and it always seems to be the User scope default biting me in the arse.
Basically, I create a setting, plug along for a while, then go back to add another setting and realize that I had left it in user scope. I switch the first setting to application scope and keep plugging away. Somewhere between then and the next time the application tries to access that property, the exception is thrown and doesn’t give you much detail at the surface level.
Figuring out the Files You Need to Delete
I said you can likely delete the user.config files without much regard. Truth is if you’re into testing something settings-related that can be a real inconvenience.
I will drill into the inner exception to find the problem file; it’s only two deep, so it’s easy to find the path of the file in question if you’re willing to dig.
Trust me, folks, your debugger is your best friend.