Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User.config corruption

So I've done quite a bit of research on trying to figure this issue out but can't seem 1) Reproduce the issue, but more importantly 2) Find an update-to-date solution for fixing it.

It's already happened twice within a span of 2 weeks where randomly the user.config file will get corrupted (e.g. there will be missing chunks of the XML file) and thus result in an application crash on next startup.

Cliffnotes:

  1. I need an up-to-date solution for dealing with this issue programmatically(i.e. whether it's the post from CodeProject Handling Of Corrupted User Config or a different post

  2. I also need a reproducible case for "corrupting" the user.config file (aside from deleting out chunks of it, as it did not work for me) to be able test #1 above.

Solutions I've come across:

  1. I know that you can manually navigate to the crazy path (.../AppData/..././1.0.0.0/user.config) and delete it and re-run the application, however, this is not an appropriate solution for us. We need to be able to programmatically catch this error, and deal with it accordingly. If possible, it'd be best to not have to restart the application.

  2. I've found a few posts/answers, that are slightly different versions of this article CodeProject Handling Of Corrupted User Config where they use either the same mechanism as that link, or use a ConfigurationManager command to open the config file and catch the exception.

What I've tried:

  1. One of my problems is that I cannot seem to access the ConfigurationManager class in C# 4.5.2. But MSDN docs say that it's available in the current framework. So I couldn't really try

  2. When I try and manually "corrupt" my user.config file to test my solutions, it gives me no issues, and the application works fine...which is alarming, as I can't find a reproducible case to test whether or not the given solution works (i.e. the one provided in the link above that does NOT use ConfigurationManager)

Any help would be greatly appreciated!

like image 877
hpradogu Avatar asked Mar 10 '17 02:03

hpradogu


1 Answers

To manually corrupt your file, just delete all of the text inside it and save the file.

In order to access the ConfigurationManager class, you have to add a DLL reference to System.Configuration. If you need help adding a reference, these are the steps listed online:

  1. In Solution Explorer, right-click the project node and click Add Reference.
  2. In the Add Reference dialog box, select the tab indicating the type of component you want to reference.
  3. Select the components you want to reference, and then click OK.

To account for the corruption, you can use a try/catch block like the following:

try {
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
}
catch (ConfigurationErrorsException exception) {
    Console.WriteLine("Settings are Corrupt!!");
    // handle error or tell user
    // to manually delete the file, you can do the following:
    File.Delete(exception.Filename); // this can throw an exception too, so be wary!
}

Edit: As a further note, note that if you have a Settings.Upgrade() call anywhere and your "old" file is corrupt, this will also crash the software. Make sure to surround your Settings.Upgrade() call with a try/catch block as well.

like image 68
Deadpikle Avatar answered Oct 04 '22 20:10

Deadpikle