.net 配置文件 AppSettings:NameValueCollection 与 KeyValueConfigurationCollection

发布于 2024-10-14 03:28:34 字数 1227 浏览 2 评论 0原文

当访问当前应用程序的appSettings时,我得到一个NameValueCollection:

NameValueCollection settings =
    ConfigurationManager.AppSettings;

当访问另一个应用程序的appSettings时,我得到一个KeyValueConfigurationCollection:

KeyValueConfigurationCollection settings = 
    ConfigurationManager.OpenExeConfiguration(sExe).AppSettings.Settings;

  1. 这两种方法是否有原因(ConfigurationManager.AppSettingsAppSettingsSection.Settings)具有相似但不同(且不兼容)的返回类型?也许我在这两种情况之一中使用了过时的方法?

  2. 有没有一种简单的方法可以在这两种情况下获得相同的类型,即为另一个应用程序的 appSettings 获取 NameValueCollection 或为当前正在运行的应用程序的 appSettings 获取 KeyValueConfigurationCollection?


更新:对于问题 2,我找到了以下方法来获取当前运行的(非 Web)应用程序的配置作为 KeyValueConfigurationCollection:

KeyValueConfigurationCollection settings = 
    Configuration.ConfigurationManager.OpenExeConfiguration(Configuration.ConfigurationUserLevel.None).AppSettings.Settings;

When accessing the current application's appSettings, I get a NameValueCollection:

NameValueCollection settings =
    ConfigurationManager.AppSettings;

When accessing another application's appSettings, I get a KeyValueConfigurationCollection:

KeyValueConfigurationCollection settings = 
    ConfigurationManager.OpenExeConfiguration(sExe).AppSettings.Settings;

  1. Is there a reason why these two methods (ConfigurationManager.AppSettings and AppSettingsSection.Settings) have similar but different (and incompatible) return types? Maybe I'm using an outdated method in one of the two cases?

  2. Is there an easy way to get the same type in both cases, i.e., to get a NameValueCollection for another application's appSettings or a KeyValueConfigurationCollection for the currently running application's appSettings?


Update: For question 2, I've found the following way to get the configuration of the currently running (non-web) application as a KeyValueConfigurationCollection:

KeyValueConfigurationCollection settings = 
    Configuration.ConfigurationManager.OpenExeConfiguration(Configuration.ConfigurationUserLevel.None).AppSettings.Settings;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈情不如逗狗 2024-10-21 03:28:34

两者都试图解决相同的问题,并且它们与相同的配置模式兼容,但区别在于,正如您所说,两者都是在不同的开发时代演变的。

但这与您使用过时的版本无关。这些是获得相同结果的不同方法。也许您不明白为什么,但关键是有时您需要从不同的来源获取配置,因此拥有这些选项是有意义的。

回答你的第二个问题,你可以为两种返回类型实现一个扩展方法,将它们转换为通用类型。

例如,如果您想要 NameValueCollection,您可以实现:

public static NameValueCollection ToCollection(this KeyValueConfigurationCollection source)
{
       // An iterator to create a NameValueCollection here and return it.
}

或者如果您想要 KeyValueConfigurationCollection,您可以执行相同的操作,但要返回此类型的实例。

然后,当您需要 AppSettings 时,您可以执行 ConfigurationManager.AppSettings.ToCollection(); 和/或 ConfigurationManager.OpenExeConfiguration(sExe).AppSettings.Settings.ToCollection();< /代码>.
检查作者的答案编辑!我的这部分答案是错误且无用的:) 谢谢。

事实上,名称-值集合在某种程度上已经过时了,因为它们来自 .net 1.x 天。但它并没有过时,因为这是(目前)这样做的方式。

Both are trying to solve the same problem and they're compatible with the same configuration schema, but the difference resides in the fact that both have evolved in different development times, as you said.

But it's not about you're using outdated versions. Those are different ways of getting the same result. Maybe you can't figure why, but the point is sometimes you need to obtain your configuration from different sources, so make sense having these options.

Answering your second question, you can implement an extension method for both return types converting them to a common type.

For example, if you want NameValueCollection you can implement that:

public static NameValueCollection ToCollection(this KeyValueConfigurationCollection source)
{
       // An iterator to create a NameValueCollection here and return it.
}

Or if you want KeyValueConfigurationCollection, you can do the same, but for returning instances of this type.

Then, when you want AppSettings, you can do ConfigurationManager.AppSettings.ToCollection(); and/or ConfigurationManager.OpenExeConfiguration(sExe).AppSettings.Settings.ToCollection();.
Check author's answer edit! This part of my answer is wrong and useless :) Thanks.

In fact, name-value collections are someway outdated because are from .net 1.x days. But it's not obsolete, because this is the way of doing it (for now).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文