30

We have created a WinForms application and store some configurations via the ConfigurationManager class. In order to store the information I use

Configuration pConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
pConfig.AppSettings.Settings.Add("updates", szUpdatesURL);
pConfig.Save(ConfigurationSaveMode.Modified);

The problem here is that in some occasions the application creates an "appname".config file and in other occasions an "appname".exe.config.

Here I must note that a configuration file is not shipped by default since it is not always required.

The configurations are saved the first time the program is executed. This has caused us a problem, and I cannot specify the occasions when which one or the other is created.

I have performed the tests, on the same pc, with the exact same .exe and I get both results. What's going on here?

What is the difference between the two, and how can I specify which one should be created? Many thanks

2
  • Is there any reason why you can't ship an empty config file?
    – Paolo
    Commented Feb 1, 2010 at 8:27
  • @Paolo Not really, but the problem is that we originally deployed this way, and the need for a configuration file, came up on a later version. So this causes some problems for users of older versions trying to upgrade. Commented Feb 1, 2010 at 8:29

1 Answer 1

39

The "appname.exe.config" is automatically created for you when you compile your application. This is the file that should be distributed to your end users (along with the exe file, of course). The settings you set in appname.config is transferred over to appname.exe.config. They are essentially the same files. The reason appname.config exists is because when the executable is run, it's config file is simple the executable's name with a .config suffix. However, if the executable's name changed, you would have to change the name of the exe.config file manually. Therefore, by automatically renaming at compile time, the app.config can change it's name to newappname.exe.config file and the CLR will still pick it up. You'll probably find that the appname.exe.config file is created in the bin directory. I hope that's clear :) The links below may explain it in slightly more depth.

There's a good explanation here. Another good read is on CodePlex.

3
  • 1
    Thanks. However my main concern is that I do not ship a config file with the exe by default. In some occasions the program creates a config file. And in these cases is where I have the problem where sometimes it creates for example "Appname".config and other times it creates "Appname".exe.config. This is what has me confused, since I understood that the default settings filename should be *.exe.config Commented Feb 1, 2010 at 8:13
  • 1
    From the tests I have performed, I did not come up with a conclusion as per when actually the "appname".config is actually generated. However it is obvious that in order to avoid such confusions, we should ship an "appname".exe.config by default. Commented Feb 1, 2010 at 9:30
  • 1
    Make sense, today I found my program runs OK in Visual Studio, but when I copy it to another directory it says "Out of memory", just because I don't have the config file in the directory
    – Benny Ae
    Commented May 4, 2020 at 14:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.