.net app.config 有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1431742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is app.config for?
提问by CannibalSmith
Settings.settingsgenerates Settings.Designer.cswhich presumably generates app.configwhich 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?
Settings.settings生成Settings.Designer.cs它大概会生成app.config然后被复制到输出目录作为Foo.exe.config. 当我在没有配置文件的情况下分发应用程序时,似乎没有什么不好的事情发生。那么,那个文件是做什么用的呢?
回答by Jon Skeet
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.
您可以拥有每个用户的设置以及每个应用程序的设置,它们存储在不同的位置。
回答by Adriaan Stander
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.
应用程序配置文件包含特定于应用程序的设置。此文件包含公共语言运行时读取的配置设置(例如程序集绑定策略、远程处理对象等),以及应用程序可以读取的设置。
回答by Ash
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.
配置文件是可选的,如果它不存在,则 ASP.NET 等环境将回退到存储在 .NET 系统目录中的 machine.config 文件以获取机器范围的默认值。
If you actually add code to your app to retrieve settings from the config file (say using the ConfigurationManagerclass) and it does not exist, you will receive null values.
如果您实际上将代码添加到您的应用程序以从配置文件中检索设置(例如使用ConfigurationManager类)并且它不存在,您将收到空值。
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.
这就是为什么重要的是检查这种情况并让您的应用程序自行决定如何/是否继续。
回答by Heiko Hatzfeld
You can store your configuration in that file.
您可以将配置存储在该文件中。
The .Net framework will automatically load a config file with the exe-name.config.
.Net 框架将自动加载一个带有 exe-name.config 的配置文件。
If you dont use any configurations in your application, then nothing bad will happen...
如果您不在应用程序中使用任何配置,则不会发生任何不好的事情...

