Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using app.config correctly

In several C# projects I have been using an app.config file to pass various settings to my program, settings like connectionstrings, power levels etc.

However sometimes I have come in situations where the settings aren't updated as expected and I have concluded that I am not that well informed with the proper use of app.config file.

Example:

  • Replacing the .exe file with a new version (where settings are different) to the output directory without changing the exe.config, results in the program seeing the hard-coded settings and not the settings of the existing .exe.config

So my Questions are:

  • What is the exact role of exe.manifest file
  • Every time I create a new .exe do I have to paste in the output folder anything else except the .exe file?
  • Whats the difference in obtaining the setting value by: ConfigurationManager.'settingName'... rather than: Properties.Settings.Default.'settingName'?
  • What is the role of app.config build action?

Sorry If I am asking too much in a single Question.

like image 877
apomene Avatar asked Apr 23 '14 10:04

apomene


1 Answers

The app.config file is a file that gets a special treatment when running the associated application. If you executable is named MyApp.exe the app.config file should be named MyApp.exe.config. The role of the app.config build task is to copy the file named app.config in your project to MyApp.exe.config in the output directory.

.NET provides ways to read the contents of the file (it is in XML format) and various parts of .NET will look for different sections in this XML to provide configuration.

A widely used section is the settings section you refer to as Properties.Settings.Default. This section plays together with Visual Studio that provides an editor for application settings. The settings are accessed in the code by using a generated class. Adding a setting will add a property to this class and this property is initialized from a value in the app.config file.

In the Visual Studio editor you can set a value for the setting and you can think of this as a default value for the setting. However, if you overwrite the value in the app.config file the later will take precedence. This allows you to modify the app.config file after installation and rerun the application with a modified setting.

You can also access application settings the app.config file using other methods, but in my oppinion using the Visual Studio editor and the code generated class is the best way to do that.

I am not sure I fully understand the problem that you experience. If you update MyApp.exe and leave MyApp.exe.config intact you should not see a change in the settings used by the application (unless of course you have renamed or changed some settings).

The manifest file provides information about side-by-side assemblies and can be used to request elevated privileges among other things. This is not related to the app.config file.

like image 82
Martin Liversage Avatar answered Oct 25 '22 14:10

Martin Liversage