What is app.config for?

2019-01-07 09:26发布

问题:

Settings.settings generates Settings.Designer.cs which presumably generates app.config which then is copied to output directory as Foo.exe.config. When I distribute the application without the config file, nothing bad seems to happen. So, what is that file for?

回答1:

If you don't have the config file, it uses the default values from the designer. However, the config file allows users/administrators to easily change settings - such as the server you talk to, themes, etc. If you don't have the file, where would you expect those settings to be stored?

You can have per-user settings as well as per-application settings, which are stored in different locations.



回答2:

Application configuration files contain settings specific to an application. This file contains configuration settings that the common language runtime reads (such as assembly binding policy, remoting objects, and so on), and settings that the application can read.



回答3:

The config file is optional, if it does not exist environments such as ASP.NET will fall back to the machine.config file stored in the .NET system directories to get machine wide defaults.

If you actually add code to your app to retrieve settings from the config file (say using the ConfigurationManager class) and it does not exist, you will receive null values.

That is why it is important to check for this siutation and have your application make it's own decision on how/if to continue.



回答4:

You can store your configuration in that file.

The .Net framework will automatically load a config file with the exe-name.config.

If you dont use any configurations in your application, then nothing bad will happen...