What is app.config for?

2019-01-07 09:23发布

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?

4条回答
可以哭但决不认输i
2楼-- · 2019-01-07 09:50

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...

查看更多
迷人小祖宗
3楼-- · 2019-01-07 09:51

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.

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-07 09:59

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.

查看更多
【Aperson】
5楼-- · 2019-01-07 10:14

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.

查看更多
登录 后发表回答