在应用程序之间共享设置

发布于 2024-09-11 16:34:36 字数 296 浏览 2 评论 0原文

我有多个 .NET 程序集,它们都需要共享通用用户设置,例如首选项、用户名等。一个是 WPF 应用程序,另一个是控制台应用程序,第三个是 Office 加载项。所有这些设置都是用户范围的。

只有 WPF 应用程序需要能够更改设置。其余的只是阅读它们。

理想情况下,我想使用 .NET 配置框架。但我不知道该怎么做。如果我将Settings添加到WPF应用程序中,其他应用程序如何找到user.config文件?

创建类库并使用isolatedfilestorage并序列化我的设置是否更容易?

任何建议将不胜感激。

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.

Only the WPF application needs to be able to change settings. The rest just read them.

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?

Is it just easier to create a class library and use IsolatedFileStorage and serialize my settings?

Any advice would be greatly appreciated.

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

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

发布评论

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

评论(2

你如我软肋 2024-09-18 16:34:36

您可以实现自定义设置类,继承 ApplicationSettingsBase。作为一个好的开始,您可以将默认的用户设置文件添加到示例项目中(右键单击项目 -> Properties -> Settings -> 此项目不包含默认设置文件。单击此处创建一个。)。添加用户范围的设置并研究设计器生成的 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 方法提供的功能。如果您不需要其中任何一个,这可能是更干净的方法。

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;
            }
        }
    }
}

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.

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.

冷了相思 2024-09-18 16:34:36

我建议您创建服务来提供和更新用户信息和/或偏好。这将是更好的架构、更干净的解决方案,并且更容易维护和扩展。

I would recommend you to create service to provide and update user info and or preferences. It will be better architecture, cleaner solution and it will be easier to maintain and extend.

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