且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

.NET 桌面应用程序中的 Settings.settings 与 app.config

更新时间:2021-11-19 09:00:25

从 .NET Framework 的角度来看(暂时不说工具 - Visual Studio -),历史上只有 [app.exe].config(其实就是AppDomain定义的配置文件,名字是AppDomain定义的,所以对于web应用来说就是web.config...)和 machine.config.'app' 与应用程序一起部署,'machine' 用于整机.对于普通用户来说,它们应该是完全"只读的.可以改变它们,但这不是我们的想法.

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.

但是我怎样才能保存最终用户的首选项呢?这就是引入 [user].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,其中user是当前人的用户名执行应用程序.您可以为用户范围的使用 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 Framework 的角度来看,只有一个 3 层机制.

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

现在,Visual Studio 只是尝试通过为最终读/写设置生成类型安全代码来帮助您.大多数情况下,[user].config 文件不存在,设置值将由 DefaultSettingValueAttribute 中的内容定义(为每个设置定义),或者使用应用程序中静态定义的内容.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.