Search code examples
c#visual-studionullapplication-settings

Visual Studio Application Settings returning a null pointer


I have an application where I use the visual studio application settings as internal. Just a couple of simple variables to save the user config. It all works fine, but I had a bug some time ago where when I tried to read the settings it would return me a null pointer exception (using C#). I believe it was due to a BSOD that could have corrupted the settings file. But then shouldnt the settings get the default values? Also, I couldnt repeat the bug, so any ideas on how I could try to repeat it will help. And one more silly question, how do I find out the settings file name and location?


Solution

  • So... the way it works is this: The settings are saved in "app.config" file in the project directory. When this file gets corrupted it is useless to reset the settings or reload. The only way to fix this is by deleting the file. This way, a new file will be created with the default settings. This should be done dynamically using a try/catch statement where the settings are first read in the application. In the catch block the corruption will be caught and the file has to be deleted and then reloaded. That should fix it.

    More details: http://www.codeproject.com/Articles/30216/Handling-Corrupt-user-config-Settings