我是否必须从 ConfigurationSection 派生才能支持每用户设置?
我正在研究 .NET 的配置支持(ConfigurationManager 类和相关支持类)。 我想编写一个应用程序,安装后:
- 在 foo.exe.config (在程序文件中)中具有默认设置。
- 用户稍后可以使用应保留的非默认值覆盖设置。
- 用户的首选项应保留在用户的配置文件中,因为他不应该具有对 Program Files 目录的写入权限。
应用程序应在设置时使用用户的首选项,否则使用默认值。
看起来这应该很简单——这是一种非常常见的模式。 但我在这方面的尝试遇到了障碍,我想知道我是否采取了正确的方法。
以下代码产生运行时异常“ConfigurationSection 属性在锁定时无法编辑”。
using System;
using System.Configuration;
namespace DemoAppSettingsProblem
{
class Program
{
static void Main(string[] args)
{
Configuration userConfig =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
if ( userConfig.AppSettings.Settings["foo"] != null )
userConfig.AppSettings.Settings.Remove("foo");
userConfig.AppSettings.Settings.Add("foo", "The string is foo.");
userConfig.Save(ConfigurationSaveMode.Modified); // exception!
}
}
}
问题在于 .NET 定义的
部分是使用默认的 allowExeDefinition=MachineToApplication
声明的(请参阅此 不错的帖子,作者:微软的 Irena Kennedy)。 这会禁止将该部分写入用户的配置文件(本地或漫游)。
因此,我假设我需要使用 allowExeDefinition=MachineToLocalUser
定义自己的部分。 但据我从 MSDN 文档得知,这意味着我需要创建自己的配置类,该类派生自 ConfigurationSection
。 那里的例子表明我要做的工作比我预期的要多,这通常会敲响我的警钟,让我知道我做错了什么。
实现这个目标真的有这么难吗? .NET 是否提供了一种简单的方法来支持这一点,或者我应该采取完全不同的方法?
I'm playing around with .NET's configuration support (the ConfigurationManager class and related support classes). I would like to write an application that, once installed:
- Has default settings in foo.exe.config (in Program Files).
- The user may later override the settings with nondefault values which should be persisted.
- The user's preferences should be persisted in the user's profile, since he shouldn't have write permissions to the Program Files directory.
The app should use the user's preferences when they're set, otherwise use the defaults.
It seems like this ought to be easy - it's a very common pattern. But my attempts at this are running into bumps and I'm wondering if I'm taking the right approach.
The following code produces the runtime exception "ConfigurationSection properties cannot be edited when locked".
using System;
using System.Configuration;
namespace DemoAppSettingsProblem
{
class Program
{
static void Main(string[] args)
{
Configuration userConfig =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
if ( userConfig.AppSettings.Settings["foo"] != null )
userConfig.AppSettings.Settings.Remove("foo");
userConfig.AppSettings.Settings.Add("foo", "The string is foo.");
userConfig.Save(ConfigurationSaveMode.Modified); // exception!
}
}
}
The problem is that the .NET-defined <appSettings>
section is declared with the default allowExeDefinition=MachineToApplication
(see this nice post by Microsoft's Irena Kennedy). This prohibits the section from being written to the user's profile (either local or roaming).
So, I assume I need to define my own section, with allowExeDefinition=MachineToLocalUser
. But as far as I can tell from the MSDN docs, that means I need to create my own configuration class, derived from ConfigurationSection
. The examples there point me toward more work than I was expecting, which usually sets off my alarm bells that I'm doing something wrong.
Is it really this difficult to achieve this? Is there a simple way .NET provides to support this, or should I perhaps be taking a different approach altogether?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您使用的是 .NET 2.0 及更高版本,您检查过“设置”文件吗? 这些存储在您的 app.config 中 -中的用户可定义设置,其中附带有
部分中的应用程序设置,allowExeDefinition=MachineToLocalUser
你提到的设置。也许这可能是一种方法? 这些用户设置是已定义的类型,如果需要,您绝对可以以不同的名称(“mySettings”)重复使用它们。
马克
Assuming you're on .NET 2.0 and higher, have you checked out the "Settings" files? Those are being stored inside your app.config - application settings in
<applicationSettings>
sections, user-definable settings in <userSettings>, which incidentally have theallowExeDefinition=MachineToLocalUser
setting you mentioned.Maybe that could be an approach? Those user settings are a defined type, which you could definitely reuse under a different name, if needed ("mySettings").
Marc
我使用了设置功能,它会将用户设置写入应用程序配置...
http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx
I've used the settings feature, it writes user settings to the app config for you...
http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx