views:

31

answers:

2

Sometimes in .NET (2.0) WinForm desktop apps which use the default configuration system, the user.config file will become corrupted and can't be loaded anymore (when the configuration system tries to load it, it'll throw a System.Xml.XmlException).

Putting aside the question of "why did the file get corrupted in the first place" (maybe it was cosmic rays, or HDD problems, or something else you can't control), the question then becomes "how can one catch this exception and do something meaningful with it?

Microsoft's Connect site has a report on this sort of problem. Unfortunately, their report isn't very helpful.

Is it possible to catch exceptions that originate in the default configuration system loader itself? Or would you have to roll your own configuration system to be able to catch this type of exception?

A: 

One easy way would be to have a "loader" app that all it does is check if the config file for the real app exists, otherwise create it, and then start the real app.

Might be some neater way of doing it that I'm not aware of though.

ho1
A loader app would not be able to get the location of the user.config file of another application - it can only locate its own user.config file.
Keithius
@Keithius: Well, yes and no, it can't automatically find .config files for other apps, but to be a loader app it needs to be told where the app is that it wants to load, and if it knows where the app is it can probably find the .config file of that app...
ho1
@ho1: Possibly, but probably not, since the default location of an app's user.config file is: <Profile Directory>\<Company Name>\<App Name>_<Evidence Type>_<Evidence Hash>\<Version>\user.config - and it's that <Evidence Hash> bit that you can't (reliably) determine on your own.
Keithius
@Keithius: My mistake, was thinking about the app.config file. But even so you could probably just search for it in the users directory in various ways. Maybe you could use `Configuration.Locations` and `ConfigurationManager.OpenExeConfiguration` to find it as well? Never done it but sounds like a possibility.
ho1
A: 

There's something wrong - something, probably your application, is writing a blank user.config file. This is a bug to investigate and fix. It may be that your application is not exiting cleanly (so it is sometimes silently wiping the app.config, so I'd start by taking a look at the shutdown sequence. Also, sticking a 3rd party file utility on the file may help you catch see exactly when and where it gets corrupted).

If you can't fix the problem, then the best workaround I can think of is to use an independent XML configuration file (ditch the automated app.config approach) - then you have complete control over when and where it is loaded and saved.

But, fundamentally, there's a big code smell and I'd want to know why the standard xml configuration approach isn't working in my app when it works fine in thousands of others.

Jason Williams
I've tried to find out *why* the user.config file is being corrupted, but this is a *very* rare bug (which I can't reproduce under controlled conditions). If it's a shutdown problem, then it's probably something like an unexpected shutdown - maybe the computer is being powered off (like, plug pulled from the wall) while the app is shutting down. Even if I rolled my own configuration system, I still wouldn't be able to stop things like this from happening. Assuming I can't stop it from happening, the best I can do is try to fix it at runtime when it does happen.
Keithius