I'm using an user.config to store my application settings. I can access the settings in C# for example with Properties.Settings.Default.Setting1 and the user.config file looks like that:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<MyApp.Properties.Settings>
<setting name="Setting1" serializeAs="String">
<value>ABCDEF</value>
</setting>
</MyApp.Properties.Settings>
</userSettings>
</configuration>
But when my user.config is corrupt for what reason ever, my application is crashing with the following System.ConfigurationErrorException.
I would prefer in that case that the application is deleting the corrupt user.config file and starts with the default settings.
Is there a way to achieve that?
Update
I've seen that this question has been asked before see user.config corruption issue and User.config corruption.
But If I try the following code
// Check for corrupt settings
try
{
var dummy = Properties.Settings.Default.Setting1;
}
catch (ConfigurationErrorsException exception)
{
Console.WriteLine("Settings are corrupt!");
File.Delete(exception.Filename);
}
the exception.Filename is null. And If I use ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal) inside my try block the exception is not raised in all cases.