在.NET 桌面应用程序中,Settings.setvs.app.config 与 app.config

可能的复制品:
在 app.config 文件和 XYZ.sets 文件之间有什么区别?

我对 Visual Studio 中存储和管理桌面应用程序设置的这两种机制的明显冗余感到非常困惑:

  • 您可以使用 XMLapp.config文件,将项目添加到 <appSettings>部分。可以使用 ConfigurationManager类从代码中检索这些。
  • 或者,您可以使用 Settings.sets 文件通过编辑器添加单独的设置。VisualStudio 将生成一个 Settings类,用于在运行时对设置进行类型安全检索。

这两种机制似乎服务于相同(或几乎相同)的目的。我知道存在一些差异,但我也对这种重叠及其后果感到困惑。例如,当我使用 VisualStudio 向 Settings.settings文件添加设置时,我输入的所有信息最终也会作为条目出现在 app.config文件中。显然,存在一种同步机制: 如果我更改了 app.config文件中的设置,Visual Studio 会提示我在下次在编辑器中打开 Settings.settings文件时更新它。

我的问题是:

  • 为什么是两个机制,而不是一个?
  • Settings.settings上使用 app.config最常见的情况是什么,反之亦然?
  • 如果我的应用程序使用 Settings.settings,并且在部署后更改了 app.config中的值,会发生什么情况?由于 Settings.settings已经被编译和分发,因此无法进行同步。

注意。我一直在寻找关于这个话题的问题,但是我更加困惑。例如,这个问题的答案是相当矛盾的,没有多少光亮。

注2。我知道 app.config是一个设计时文件名,并且熟悉 Visual Studio 将其复制并重命名为可执行文件夹的动态过程。

48434 次浏览

Why two mechanisms and not just one?

They serve different purposes. The settings API offers read/write access from the application, whereas config is read only (unless you write the file in code).

Settings can be defined per user or per application, and are designed to be volatile. User settings are written to hidden folder within User Profile storage which is permitted under UAC.

App.config is per application only. Changes to App.config aren't automatically picked up. It requires restart or code to refresh the values. Under UAC users are not permitted to write to the application directories such as Program Files, so this file should be considered static readonly.

What are the most common scenarios for using app.config over Settings.settings, and vice versa?

You could use Settings in a desktop application for storing user preferences, or settings that change at runtime.

You would use App.config for more generic static settings, like connection strings etc, or for defining the configuration of components used within your app.

What happens if my app is using Settings.settings and I change a value in app.config after it's been deployed?

If the application is redeployed then it will pick up the new settings, unless there are user/app customisations on the machine already in which case it will continue to use those, unless you wipe them.

If you add new settings, these will get picked up. In fact the default values are baked into the Settings class, so even if the app.config is empty the Settings still function.

From a .NET Framework point of view (not speaking of tools - Visual Studio - for the moment), historically, there was only [app.exe].config (in fact, it's what the AppDomain defines as the configuration file. The name is defined by the AppDomain, that's why it's web.config for web apps...) and machine.config. 'app' is deployed together with the application, 'machine' is for the whole machine. They were supposed to be 'quite' read-only for the average user. It's possible to change them, but it was not the idea.

But how can I save end user preferences then? That's why [user].config was introduced (I believe with .NET 2). The official documentation says this:

The configuration system that was originally released with the .NET Framework supports providing static application configuration data through either the local computer's machine.config file or within an app.exe.config file that you deploy with your application. The LocalFileSettingsProvider class expands this native support in the following ways:

1) Application-scoped settings can be stored in either the machine.config or app.exe.config files. Machine.config is always read-only, while app.exe.config is restricted by security considerations to read-only for most applications.

2) User-scoped settings can be stored in app.exe.config files, in which case they are treated as static defaults.

3) Non-default user-scoped settings are stored in a new file, user.config, where user is the user name of the person currently executing the application. You can specify a default for a user-scoped setting with DefaultSettingValueAttribute. Because user-scoped settings often change during application execution, user.config is always read/write.

So from a .NET Framework point of view, there is only one 3-layer mechanism.

Now, Visual Studio just tries to help you by generating the type-safe code for the final read/write settings. Most of the time, that [user].config file does not exists and a setting value will be defined by what's in the DefaultSettingValueAttribute (defined for each setting), or use what's been defined statically in the app.config. That's why Visual Studio also updates the app.config file so you can define static defaults to the settings. But you can perfectly delete all that app.config stuff.