简体   繁体   English

C#.NET 中的 App.config 是什么? 如何使用它?

[英]What is App.config in C#.NET? How to use it?

I have done a project in C#.NET where my database file is an Excel workbook.我在 C#.NET 中完成了一个项目,其中我的数据库文件是 Excel 工作簿。 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.由于连接字符串的位置在我的编码中是硬编码的,所以在我的系统中安装它是没有问题的,但对于其他系统是有的。

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 是什么以及如何在我的上下文中使用它?

At its simplest, the app.config is an XML file with many predefined configuration sections available and support for custom configuration sections.简而言之,app.config 是一个 XML 文件,具有许多可用的预定义配置部分,并支持自定义配置部分。 A "configuration section" is a snippet of XML with a schema meant to store some type of information. “配置部分”是 XML 片段,其架构旨在存储某种类型的信息。

Settings can be configured using built-in configuration sections such as connectionStrings or appSettings .可以使用内置配置部分(例如connectionStringsappSettings配置设置。 You can add your own custom configuration sections;您可以添加自己的自定义配置部分; this is an advanced topic, but very powerful for building strongly-typed configuration files.这是一个高级主题,但对于构​​建强类型配置文件非常有用。

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, eg 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 ConfigurationManager class.定义 app.config 后,您可以使用ConfigurationManager类在代码中读取它。 Don't be intimidated by the verbose MSDN examples;不要被冗长的 MSDN 示例吓倒; it's actually quite simple.其实很简单。

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 time which describes how to update the connectionStrings section of the *.config file at runtime.请参阅:在运行时更改连接字符串和重新加载 app.config,它描述了如何在运行时更新 *.config 文件的connectionStrings部分。

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.:假设我手动更改了 app.config 中的一些<value> ,保存然后关闭它。 Now when I go to my bin folder and launch the .exe file from here, why doesn't it reflect the applied changes?现在,当我转到 bin 文件夹并从此处启动 .exe 文件时,为什么它不反映应用的更改?

A : When you compile an application, its app.config is copied to the bin directory 1 with a name that matches your exe. A : 当你编译一个应用程序时,它的 app.config 会被复制到 bin 目录1 中,并使用与你的 exe 匹配的名称。 For example, if your exe was named "test.exe", there should be a "text.exe.config" in your bin directory.例如,如果您的 exe 名为“test.exe”,则您的 bin 目录中应该有一个“text.exe.config”。 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.您无需重新编译即可更改配置,但您需要编辑在编译时创建的配置文件,而不是原始 app.config。

1: Note that web.config files are not moved, but instead stay in the same location at compile and deployment time. 1:请注意,web.config 文件不会移动,而是在编译和部署时保持在同一位置。 One exception to this is when a web.config is transformed .一个例外是 web.config 被转换时

.NET Core .NET 核心

New configuration options were introduced with .NET Core. .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. *.config 文件的工作方式似乎没有改变,但开发人员可以自由选择新的、更灵活的配置范例。

Simply, App.config is an XML based 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 ConfigurationManager as shown in the piece of code snippet below:您可以使用ConfigurationManager访问ConfigurationManager ,如下面的代码片段所示:

var value = System.Configuration.ConfigurationManager.AppSettings["key"];
// value is now "test"

Note: ConfigurationSettings is obsolete method to retrieve configuration information.注意: ConfigurationSettings是检索配置信息的过时方法。

var value = System.Configuration.ConfigurationSettings.AppSettings["key"];

App.Config is an XML file that is used as a configuration file for your application. App.Config 是一个 XML 文件,用作应用程序的配置文件。 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.它通常用于存储连接字符串。

See this MSDN article on how to do that.请参阅有关如何执行此操作的 MSDN 文章

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 eg 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".我从 Visual Studio Code 开始,并用“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"您可能需要添加一个 nuget 包才能读取配置文件,如果是 .NET CORE,它将是“dotnet add package System.Configuration.ConfigurationManager --version 4.5.0”

You can access keys in the App.Config using:您可以使用以下方法访问App.Config 中的密钥:

ConfigurationSettings.AppSettings["KeyName"]

Take alook at this Thread看看这个线程

Just adding one more point再补充一点

Using app.config some how you can control application access, you want apply particular change to entire application use app config file and you can access the settings like below ConfigurationSettings.AppSettings["Key"]使用 app.config 一些您可以控制应用程序访问的方式,您希望将特定更改应用于整个应用程序使用应用程序配置文件,您可以访问如下所示的设置 ConfigurationSettings.AppSettings["Key"]

Application settings enable you to store application information dynamically.应用程序设置使您能够动态地存储应用程序信息。 Settings allow you to store information on the client computer that shouldn't be included in the application code (for example a connection string), user preferences, and other information you need at runtime.设置允许您在客户端计算机上存储不应包含在应用程序代码中的信息(例如连接字符串)、用户首选项以及您在运行时需要的其他信息。

  • To add an application configuration file to a C# project:要将应用程序配置文件添加到 C# 项目:

In Solution Explorer, right-click the project node, and then select Add > New Item.在解决方案资源管理器中,右键单击项目节点,然后单击 select 添加 > 新项目。 The Add New Item dialog box appears.出现添加新项目对话框。 Expand Installed > Visual C# Items.展开已安装 > Visual C# 项目。 In the middle pane, select the Application Configuration File template.在中间窗格中,select 是应用程序配置文件模板。 Select the Add button. Select 添加按钮。 A file named App.config is added to your project.一个名为 App.config 的文件将添加到您的项目中。

take a look at this article看看这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM