对 .Net 应用程序设置框架感到困惑
作为 .Net 世界的新手,我对在 WPF 应用程序中存储应用程序设置的位置和方式感到非常困惑。
我不断看到对 Properties.Settings.Default
的引用,但这在我的 WPF 应用程序中无法解析,而且我无法找到这些实际的类,以便找到要引入的程序集。这是一个WinForms 唯一的技术?
我还发现了 ConfigurationSettings
,它似乎已被弃用,以及 ConfigurationManager
、AppSettings
、ApplicationSettings
和 设置
!我确信其中许多可以一起工作,但我很难区分哪些内容属于一起,哪些内容是旧的,或者与 WPF 无关。
谁能说出新手的基本选项?
Being fairly new to the .Net world, I am getting very confused about where and how to store my application settings in a WPF app.
I keep seeing references to Properties.Settings.Default
, but that does not resolve in my WPF app, and I cannot find what actual classes these are in order to find which assembly to pull in. Is this a WinForms only technique?
I have also found ConfigurationSettings
, which appears to be deprecated, and ConfigurationManager
, AppSettings
, ApplicationSettings
, and Settings
! I'm sure many of these work together but I'm having real trouble sorting out what belongs together, and what is old, or irrelevant to WPF.
Can anyone spell out the basic options for a newbie?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
令人惊讶的是没有人能够回答这个问题,所以我做了更多的挖掘,并将在这里记录我的发现,以防其他人像我一样困惑:
ConfigurationSettings
死了。 (<= .Net 1.1)。它用于通过AppSettings
成员公开一组名称/值对。ConfigurationManager
是替代品(> .Net 2)。它位于 System.Configuration.dll 中,是所有新配置内容的基础。它是一个静态类,因此可以在任何地方访问。它还公开了一组名为AppSettings
的名称/值对,大概是为了向后兼容。AppSettings
不是类型安全的,它实际上是字符串对。ApplicationSettings
似乎指的是ApplicationSettingsBase
,它是用作较新样式类型安全设置基础的类。子类包含映射到设置的成员。ConfigurationManager
仍然用于管理/序列化这些。Settings
是上述工具生成的静态子类,它是通过将“设置文件”添加到项目中生成的,它允许设计时编辑设置。它会生成到项目的 Properties 命名空间中,因此可以通过应用中任何位置的Properties.Settings
进行访问。尽管整个文档中都提到了 WinForms,但它似乎也可以在 WPF 中使用。如果您不想使用设置文件,也可以使用您自己的自定义类直接使用ConfigurationManager。有关使用概述的一些综合链接如下:
揭开 .NET 2.0 配置的神秘面纱
解密 .NET 2.0 的奥秘配置
破解.NET 2.0 配置之谜
所有这些设置都会出现序列化到 app.config 文件,尽管我已经看到了对单独的 Applicationsettings 文件的引用,所以我对此可能是错误的。
Surprised that no-one able to answer this, so I have done some more digging and will document my findings here in case anyone else is as confused as me:
ConfigurationSettings
is dead. (<= .Net 1.1). It used to expose a set of name/value pairs viaAppSettings
member.ConfigurationManager
is the replacement (> .Net 2). It lives inSystem.Configuration.dll
, and is the basis of all new configuration stuff. It is a static class so can be accessed anywhere. It also exposes a set of name/value pairs calledAppSettings
, presumably for backwards compatibility.AppSettings
is not type safe, it's literally pairs of strings.ApplicationSettings
seems to refer toApplicationSettingsBase
, which is a class used as a base for newer style type safe settings. Subclasses contain members that map to a setting.ConfigurationManager
is still used to manage / serialise these.Settings
is a tool generated static subclass of the above that is generated by adding a "Settings File" to your project, which allows design time editing of the settings. It gets generated into the Properties namespace of your project, hence can be accessed viaProperties.Settings
anywhere in your app. Although WinForms is mentioned throughout the docs, it appears to be useable from WPF too.If you don't want to use a settings file, ConfigurationManager can also be used directly using your own custom classes. Some comprehensive links for an overview of use are:
Unraveling the Mysteries of .NET 2.0 Configuration
Decoding the Mysteries of .NET 2.0 Configuration
Cracking the Mysteries of .NET 2.0 Configuration
All these settings appear to get serialsied to app.config file, although I've seen references to separate Applicationsettings files so I may be wrong about that.
如果没有很多设置需要保存,为什么不使用 Windows 注册表呢?
文件系统和注册表(C# 编程指南)
< 没有什么神奇的code>ConfigurationManager 等,您可以只使用自己的设置对象并将其序列化/反序列化到 XML。
请参阅Application 类了解应用程序数据路径是。
至于
Properties.Settings.Default
在您的应用程序中未解析,Solution Explorer
->您的项目 ->添加
->新项目
->设置文件
。这将自动生成Properties.Settings
类,允许您在设计时编辑应用程序设置。然后您将能够调用Properties.Settings.Default.Save()If there's not a lot of settings to save, why not use Windows registry?
File System and the Registry (C# Programming Guide)
There's nothing magical about
ConfigurationManager
etc. you could just use your own settings object and serialize/deserialize it to, and from XML.Refer to the Application class to learn what the application data path is.
As for
Properties.Settings.Default
not resolving in your application,Solution Explorer
-> your project ->Add
->New Item
->Settings File
. This will autogenerate theProperties.Settings
class, allowing you to edit your application settings in design time. Then you will be able to callProperties.Settings.Default.Save()