Settings.settings主场迎战的app.config在.NET桌面应用程序 [英] Settings.settings vs. app.config in .NET desktop app

查看:314
本文介绍了Settings.settings主场迎战的app.config在.NET桌面应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/909688/what-is-the-difference-between-app-config-file-and-xyz-settings-file">What在app.config文件和XYZ.settings之间的差异文件?

我感到相当在Visual Studio中这两种机制的明显冗余混淆,以存储和管理桌面应用程序设置:

I am quite confused by the apparent redundancy of these two mechanisms in Visual Studio to store and manage desktop application settings:

  • 您可以使用XML 的app.config 文件,将项目添加到&LT;的appSettings&GT; 部分。这些可以从code。使用 ConfigurationManager中类检索。
  • 另外,你可以使用Settings.settings文件通过一个编辑器中添加单独的设置。 Visual Studio将生成一个设置类的设置类型安全的检索在运行时。
  • You can use the XML app.config file, adding items to the <appSettings> section. These can be retrieved from code using the ConfigurationManager class.
  • Alternatively, you can use the Settings.settings file to add individual settings through an editor. Visual Studio will generate a Settings class for type-safe retrieval of settings at run-time.

这些两种机制似乎起到相同(或几乎相同)的目的。我知道有一些差异,但我也困惑的是重叠及其后果。例如,当我使用Visual Studio设置添加到 Settings.settings 文件,所有我把最终在条目信息的的app.config 文件中。很显然,一个同步机制的存在:如果我改变了的app.config 文件中的设置时,Visual Studio提示我更新 Settings.settings 提交下一次我在编辑器中打开它。

These two mechanisms seem to serve the same (or nearly the same) purpose. I am aware there are some differences, but I am also puzzled by the overlap and its consequences. For example, when I use Visual Studio to add settings to the Settings.settings file, all the information that I put in ends up as entries in the app.config file as well. Apparently, a synchronisation mechanism exists: if I change a setting in the app.config file, Visual Studio prompts me to update the Settings.settings file next time I open it up in the editor.

我的问题是:

  • 为什么两种机制,而不是只有一个?
  • 什么是使用的app.config Settings.settings 最常见的情况,反之亦然?
  • 如果我的应用程序使用情况 Settings.settings 我更改的app.config 值时,它的后部署? Settings.settings 无syncronisation可能发生,因为它已经被编发。
  • Why two mechanisms and not just one?
  • What are the most common scenarios for using app.config over Settings.settings, and vice versa?
  • What happens if my app is using Settings.settings and I change a value in app.config after it's been deployed? No syncronisation of Settings.settings can happen since it's already been compiled and distributed.

请注意。我已经寻找关于这个主题的问题,但我更糊涂了。例如,回答这个问题,这里是相当矛盾的,不脱落的光线。

Note. I have searched for questions on this topic, but I am even more confused. For example, answers to this question here are quite contradictory and do not shed much light.

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

Note 2. I am aware that app.config is a design-time file name, and I am familiar with the dynamics of Visual Studio copying and renaming it to the executable folder.

推荐答案

从一个.NET框架点(不说话的工具 - Visual Studio的 - 目前),历史上,只有 [APP.EXE]的.config (事实​​上,这是什么AppDomain中定义为配置文件。这个名字是由AppDomain中定义的,这就是为什么它是的web.config 的Web应用程序...)和 machine.config的。 应用程序是与应用程序一起部署,'机'是整台机器。他们被认为是相当只读对于普通用户。这是可以改变他们,但它并没有这个想法。

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.

但我怎么能节省终端用户preferences呢?这就是为什么[用户]的.config介绍(我相信,随着.NET 2)。官方文件说的:

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:

这是最初发布的.NET配置系统   框架支持提供静态应用程序配置数据   或者通过本地计算机的Machine.config文件内   部署与您的应用程序app.exe.config文件。该   LocalFileSettingsProvider类扩展在这个原生支持   以下几种方式:

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)应用程序作用域的设置可以存储在任一在machine.config   或app.exe.config文件。 Machine.config中始终是只读的,而   app.exe.config是出于安全考虑限制为只读   对于大多数应用

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)用户作用域设置可以存储在app.exe.config文件,其中   情况下,它们被视为静态默认值。

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

3)非默认的用户范围设置存储在一个新的文件,   user.config,其中,用户是人的当前用户名   执行该应用程序。您可以为用户范围指定一个默认   与DefaultSettingValueAttribute设置。由于用户范围   设置通常应用程序执行期间发生变化,user.config是   始终读/写。

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.

所以从一个.NET框架点,只存在一个3层机制

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

现在,Visual Studio的只是试图帮助您生成类型安全的code进行最后的读/写设置。在大多数情况下,是[用户] config文件不存在,并且设定值将被什么在 DefaultSettingValueAttribute (定义每个设置),或者用什么来定义在App.config中定义静态。这就是为什么Visual Studio中还更新app.config文件,所以你可以定义静态的默认设置。但是你可以完全删除所有的app.config的东西。

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.

这篇关于Settings.settings主场迎战的app.config在.NET桌面应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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