C#.NET 中的 App.config 是什么?如何使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13043530/
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 in C#.NET? How to use it?
提问by shyam
I have done a project in C#.NET where my database file is an Excel workbook. Since the location of the connection string is hard coded in my coding, there is no problem for installing it in my system, but for other systems there is.
我在 C#.NET 中完成了一个项目,其中我的数据库文件是 Excel 工作簿。由于连接字符串的位置在我的编码中是硬编码的,所以在我的系统中安装它没有问题,但对于其他系统则有问题。
Is there a way to prompt the user to set a path once after the setup of the application is completed?
有没有办法在应用程序设置完成后提示用户设置一次路径?
The answers I got was "Use App.Config"... can anyone tell what is this App.config and how to use it in my context here?
我得到的答案是“使用 App.Config”……谁能告诉我这个 App.config 是什么以及如何在我的上下文中使用它?
回答by Tim Medora
At its simplest, the app.config is an XML file with many predefined configuration sections available and support for custom configuration sections. A "configuration section" is a snippet of XML with a schema meant to store some type of information.
简而言之,app.config 是一个 XML 文件,具有许多可用的预定义配置部分,并支持自定义配置部分。“配置部分”是 XML 片段,其架构旨在存储某种类型的信息。
Settings can be configured using built-in configuration sections such as connectionStringsor appSettings. You can add your own custom configuration sections; this is an advanced topic, but very powerful for building strongly-typed configuration files.
可以使用内置配置部分(例如connectionStrings或 )配置设置appSettings。您可以添加自己的自定义配置部分;这是一个高级主题,但对于构建强类型配置文件非常有用。
Web applications typically have a web.config, while Windows GUI/service applications have an app.config file.
Web 应用程序通常有一个 web.config,而 Windows GUI/服务应用程序有一个 app.config 文件。
Application-level config files inherit settings from global configuration files, e.g. the machine.config.
应用程序级配置文件从全局配置文件继承设置,例如 machine.config。
Reading from the App.Config
从 App.Config 读取
Connection strings have a predefined schema that you can use. Note that this small snippet is actually a valid app.config (or web.config) file:
连接字符串具有您可以使用的预定义架构。请注意,这个小片段实际上是一个有效的 app.config(或 web.config)文件:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="MyKey"
connectionString="Data Source=localhost;Initial Catalog=ABC;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
Once you have defined your app.config, you can read it in code using the ConfigurationManagerclass. Don't be intimidated by the verbose MSDN examples; it's actually quite simple.
定义 app.config 后,您可以使用ConfigurationManager类在代码中读取它。不要被冗长的 MSDN 示例吓倒;其实很简单。
string connectionString = ConfigurationManager.ConnectionStrings["MyKey"].ConnectionString;
Writing to the App.Config
写入 App.Config
Frequently changing the *.config files is usually not a good idea, but it sounds like you only want to perform one-time setup.
频繁更改 *.config 文件通常不是一个好主意,但听起来您只想执行一次性设置。
See: Change connection string & reload app.config at run timewhich describes how to update the connectionStringssection of the *.config file at runtime.
请参阅:在运行时更改连接字符串和重新加载 app.config,它描述了如何在运行时更新connectionStrings*.config 文件的部分。
Note that ideally you would perform such configuration changes from a simple installer.
请注意,理想情况下,您将从简单的安装程序中执行此类配置更改。
Location of the App.Config at Runtime
App.Config 在运行时的位置
Q: Suppose I manually change some <value>in app.config, save it and then close it. Now when I go to my bin folder and launch the .exe file from here, why doesn't it reflect the applied changes?
问:假设我<value>在 app.config 中手动更改了一些,保存然后关闭它。现在,当我转到 bin 文件夹并从此处启动 .exe 文件时,为什么它不反映应用的更改?
A: When you compile an application, its app.config is copied to the bin directory1with a name that matches your exe. For example, if your exe was named "test.exe", there should be a "text.exe.config" in your bin directory. You can change the configuration without a recompile, but you will need to edit the config file that was created at compile time, not the original app.config.
A: 当你编译一个应用程序时,它的 app.config 会被复制到 bin 目录1 中,并使用与你的 exe 匹配的名称。例如,如果您的 exe 名为“test.exe”,则您的 bin 目录中应该有一个“text.exe.config”。您无需重新编译即可更改配置,但您需要编辑在编译时创建的配置文件,而不是原始 app.config。
1: Note that web.config files are not moved, but instead stay in the same location at compile and deployment time. One exception to this is when a web.config is transformed.
1:请注意,web.config 文件不会移动,而是在编译和部署时保持在同一位置。一个例外是 web.config 被转换时。
.NET Core
.NET 核心
New configuration options were introduced with .NET Core. The way that *.config files works does not appear to have changed, but developers are free to choose new, more flexible configuration paradigms.
.NET Core 引入了新的配置选项。*.config 文件的工作方式似乎没有改变,但开发人员可以自由选择新的、更灵活的配置范例。
回答by System Down
App.Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.
App.Config 是一个 XML 文件,用作应用程序的配置文件。换句话说,您可以在其中存储您可能想要更改的任何设置,而无需更改代码(和重新编译)。它通常用于存储连接字符串。
See this MSDN articleon how to do that.
请参阅有关如何执行此操作的 MSDN 文章。
回答by CloudyMarble
You can access keys in the App.Configusing:
您可以使用以下方法访问App.Config 中的密钥:
ConfigurationSettings.AppSettings["KeyName"]
Take alook at this Thread
看看这个线程
回答by Furqan Safdar
Simply, App.configis an XMLbased file format that holds the Application Level Configurations.
简而言之,App.config是一种XML基于文件格式,用于保存应用程序级别配置。
Example:
例子:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="key" value="test" />
</appSettings>
</configuration>
You can access the configurations by using ConfigurationManageras shown in the piece of code snippet below:
您可以使用ConfigurationManager以下代码片段中所示的方式访问配置:
var value = System.Configuration.ConfigurationManager.AppSettings["key"];
// value is now "test"
Note:ConfigurationSettingsis obsolete method to retrieve configuration information.
注意:ConfigurationSettings是过时的检索配置信息的方法。
var value = System.Configuration.ConfigurationSettings.AppSettings["key"];
回答by Dantel35
Just to add something I was missing from all the answers - even if it seems to be silly and obvious as soon as you know:
只是添加一些我从所有答案中遗漏的东西 - 即使你一知道它似乎很愚蠢和明显:
The file has to be named "App.config" or "app.config" and can be located in your project at the same level as e.g. Program.cs.
该文件必须命名为“App.config”或“app.config”,并且可以位于与 Program.cs 相同级别的项目中。
I do not know if other locations are possible, other names (like application.conf, as suggested in the ODP.net documentation) did not work for me.
我不知道其他位置是否可行,其他名称(如 ODP.net 文档中建议的 application.conf)对我不起作用。
PS. I started with Visual Studio Code and created a new project with "dotnet new". No configuration file is created in this case, I am sure there are other cases. PPS. You may need to add a nuget package to be able to read the config file, in case of .NET CORE it would be "dotnet add package System.Configuration.ConfigurationManager --version 4.5.0"
附注。我从 Visual Studio Code 开始,并使用“dotnet new”创建了一个新项目。在这种情况下没有创建配置文件,我相信还有其他情况。聚苯乙烯。您可能需要添加一个 nuget 包才能读取配置文件,如果是 .NET CORE,它将是“dotnet add package System.Configuration.ConfigurationManager --version 4.5.0”

