I have noticed on a few machines in which my application's user.config file is somehow becoming corrupted and is empty when opening. I can't seem to figure out why this is happened. Is there a common thing that would cause this? Any way to safely prevent this?
My second question is how to do I restore the state? I catch the exception and delete the user.config file, but I cannot find a way to restore the configuration without restarting the application. Everything I do on the Properties object causes the following error:
"Configuration system failed to initialize"
Reset, Reload, and Upgrade all do nothing to solve the problem.
Here is my code for deleting after exception:
catch (System.Configuration.ConfigurationErrorsException ex)
{
string fileName = "";
if (!string.IsNullOrEmpty(ex.Filename))
fileName = ex.Filename;
else
{
System.Configuration.ConfigurationErrorsException innerException = ex.InnerException as System.Configuration.ConfigurationErrorsException;
if (innerException != null && !string.IsNullOrEmpty(innerException.Filename))
fileName = innerException.Filename;
}
if (System.IO.File.Exists(fileName))
System.IO.File.Delete(fileName);
}