在应用程序之间共享设置 [英] Sharing settings between applications
问题描述
我有多个.NET程序集,它们都需要共享通用的用户设置,例如首选项,用户名等.一个是WPF应用程序,另一个是控制台应用程序,第三个是Office加载项.所有这些设置都是用户范围的.
I have multiple .NET assemblies that all need to share common user settings, such as preferences, user names, etc. One is a WPF application, another is a console application, and the third is an Office Add-in. All of these settings are user-scope.
仅WPF应用程序需要能够更改设置.其余的只是读他们.
Only the WPF application needs to be able to change settings. The rest just read them.
理想情况下,我想使用.NET配置框架.我不确定如何执行此操作.如果将设置添加到WPF应用程序,其他应用程序如何找到user.config文件?
Ideally, I'd like to use the .NET configuration framework. I'm not sure how to do this though. If I add Settings to the WPF application, how can the other applications find the user.config file?
创建类库并使用IsolatedFileStorage并序列化设置会更容易吗?
Is it just easier to create a class library and use IsolatedFileStorage and serialize my settings?
任何建议将不胜感激.
推荐答案
您可以继承 ApplicationSettingsBase .首先,您可以将默认的用户设置"文件添加到示例项目中(右键单击该项目-> Properties
-> Settings
-> This project does not contain a default settings file. Click here to create one.
).添加用户范围的设置,并研究设计器生成的Settings.Designer.cs文件的结构:
You can implement your custom settings class, inheriting ApplicationSettingsBase. As a good start, you can add the default User Settings file to a sample project (Right click on the project -> Properties
-> Settings
-> This project does not contain a default settings file. Click here to create one.
). Add a user-scoped setting and investigate the structure of the designer-generated Settings.Designer.cs file:
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类实现为内部带有内部setter的类,仅对所需的程序集或满足您需要的任何类可见.
In your custom implementation, you will not be limited to the designer-generated access modifiers, so you can implement the Settings class as internal with internal setters, visible only to the needed assemblies, or whatever fits your needs.
当然,您始终可以实现自定义的序列化/反序列化机制,但是您将失去ApplicationSettingsBase的Updgrade,Reload和Reset方法提供的功能.如果您不需要这些,这可能是一种更清洁的方法.
Of course, you can always implement your custom serialize/deserialize mechanism, but you will lose the funcionality provided by ApplicationSettingsBase's Updgrade, Reload, and Reset methods. If you don't need any of these, this could be the cleaner approach.
这篇关于在应用程序之间共享设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!