C#.NET 中的 App.config 是什么?如何使用它? [英] What is App.config in C#.NET? How to use it?

查看:33
本文介绍了C#.NET 中的 App.config 是什么?如何使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C#.NET 中完成了一个项目,其中我的数据库文件是 Excel 工作簿.由于连接字符串的位置在我的编码中是硬编码的,所以在我的系统中安装它没有问题,但对于其他系统则有.

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.

有没有办法在应用设置完成后提示用户设置一次路径?

Is there a way to prompt the user to set a path once after the setup of the application is completed?

我得到的答案是使用 App.Config"...谁能告诉我这个 App.config 是什么以及如何在我的上下文中使用它?

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 是一个 XML 文件,其中包含许多可用的预定义配置部分,并支持自定义配置部分.配置部分"是 XML 片段,其架构旨在存储某种类型的信息.

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.

可以使用内置的配置部分来配置设置,例如 connectionStringsappSettings.您可以添加自己的自定义配置部分;这是一个高级主题,但对于构​​建强类型配置文件非常有用.

Settings can be configured using built-in configuration sections such as connectionStrings or appSettings. You can add your own custom configuration sections; this is an advanced topic, but very powerful for building strongly-typed configuration files.

Web 应用程序通常有一个 web.config,而 Windows GUI/服务应用程序有一个 app.config 文件.

Web applications typically have a web.config, while Windows GUI/service applications have an app.config file.

应用程序级配置文件从全局配置文件继承设置,例如machine.config.

Application-level config files inherit settings from global configuration files, e.g. the machine.config.

连接字符串具有您可以使用的预定义架构.请注意,这个小片段实际上是一个有效的 app.config(或 web.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:

<?xml version="1.0"?>
<configuration>
    <connectionStrings>   
        <add name="MyKey" 
             connectionString="Data Source=localhost;Initial Catalog=ABC;"
             providerName="System.Data.SqlClient"/>
    </connectionStrings>
</configuration>

定义 app.config 后,您可以使用 ConfigurationManager 类.不要被冗长的 MSDN 示例吓倒;其实很简单.

Once you have defined your app.config, you can read it in code using the ConfigurationManager class. Don't be intimidated by the verbose MSDN examples; it's actually quite simple.

string connectionString = ConfigurationManager.ConnectionStrings["MyKey"].ConnectionString;

写入 App.Config

频繁更改 *.config 文件通常不是一个好主意,但听起来您只想执行一次性设置.

Writing to the 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.

请参阅:更改连接字符串 &在运行时重新加载 app.config,它描述了如何在运行时更新 *.config 文件的 connectionStrings 部分.

See: Change connection string & reload app.config at run time which describes how to update the connectionStrings section of the *.config file at runtime.

请注意,理想情况下,您可以通过简单的安装程序执行此类配置更改.

Note that ideally you would perform such configuration changes from a simple installer.

Q:假设我手动更改了 app.config 中的一些 ,保存并关闭它.现在,当我转到我的 bin 文件夹并从这里启动 .exe 文件时,为什么它不反映应用的更改?

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?

A:当你编译一个应用程序时,它的 app.config 被复制到 bin 目录1 中,名称与你的 exe 匹配.例如,如果您的 exe 名为test.exe",则您的 bin 目录中应该有一个text.exe.config".您无需重新编译即可更改配置,但您需要编辑在编译时创建的配置文件,而不是原始 app.config.

A: When you compile an application, its app.config is copied to the bin directory1 with 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.

1:请注意,web.config 文件不会移动,而是在编译和部署时保持在同一位置.一个例外是当 web.config 被 转换时.

.NET Core 引入了新的配置选项.*.config 文件的工作方式似乎没有改变,但开发人员可以自由选择新的、更灵活的配置范例.

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.

这篇关于C#.NET 中的 App.config 是什么?如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆