在应用程序之间共享设置
我有多个.NET程序集,都需要共享常用用户设置,例如首选项,用户名等。一个是WPF应用程序,另一个是控制台应用程序,第三个是Office Add -在。所有这些设置都是用户范围的。在应用程序之间共享设置
只有WPF应用程序需要能够更改设置。其余的只是阅读它们。
理想情况下,我想使用.NET配置框架。我不知道如何做到这一点。如果我将设置添加到WPF应用程序,其他应用程序如何找到user.config文件?
创建类库并使用IsolatedFileStorage并序列化我的设置更容易吗?
任何意见将不胜感激。
我会建议您创建服务来提供和更新用户信息和/或首选项。这将会是更好的架构,更清洁的解决方案,并且更容易维护和扩展。
您可以实现您的自定义设置类,继承ApplicationSettingsBase。作为一个好的开始,您可以将默认用户设置文件添加到示例项目中(右键单击项目 - >Properties
- >Settings
- >This project does not contain a default settings file. Click here to create one.
)。添加一个用户范围的设置和调查设计器生成的Settings.Designer.cs文件的结构:
namespace ConsoleApplication1.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("John Doe")]
public string Name {
get {
return ((string)(this["Name"]));
}
set {
this["Name"] = value;
}
}
}
}
在你定制的实现,你会不会局限于设计器生成的访问修饰符,这样你就可以使用内部设置器将Settings类作为内部实现,只对所需的程序集可见,或者任何符合您需要的内容。
当然,您可以始终实现您的自定义序列化/反序列化机制,但您将失去ApplicationSettingsBase的Updgrade,Reload和Reset方法提供的功能。如果你不需要任何这些,这可能是更清洁的方法。