在用户之间共享可写设置

发布于 2025-01-05 02:37:54 字数 247 浏览 1 评论 0原文

我想使用 .NET 设置设计器/框架在设计时创建应用程序设置,并让应用程序在运行时可写入这些设置。似乎开箱即用,我无法创建可以由应用程序更改并且所有用户在运行应用程序时都可以读取的设置?

我的代码只允许所有用户使用一个应用程序实例,因此冲突不是问题。

到目前为止,我认为我需要创建一个自定义的 SettingsProvider。我希望我能以某种方式继承 LocalFileSettingsProvider 并覆盖文件位置,但找不到方法来做到这一点。

I'd like to use the .NET settings designer/framework to create application settings at design time and have these settings writeable by the application at runtime. It seems out of the box I can't create settings that can be changed by the application and that are read by all users when they run the the application?

I have code that only allows one instance of the application across all users so conflict is not an issue.

So far I think I need to create a custom SettingsProvider. I'm hoping I can somehow inherit from LocalFileSettingsProvider and overwrite the file locations, but can't find a way to do this.

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

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

发布评论

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

评论(1

×眷恋的温暖 2025-01-12 02:37:54

我就是这样做的。现在我需要知道的是我是否需要实现 IApplicationSettingsProvider 才能在版本之间成功迁移设置?欢迎同行评审和评论!

using System.Collections.Generic;
using System.Configuration;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.IO.IsolatedStorage;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace NetworkManager
{
    class IsolatedStorageSettingsProvider : SettingsProvider
    {
        public IsolatedStorageSettingsProvider()
        {

        }

        public override string ApplicationName
        {
            get { return Application.ProductName; }
            set { }
        }

        public override void Initialize(string name, NameValueCollection col)
        {
            base.Initialize(this.ApplicationName, col);
        }

        // SetPropertyValue is invoked when ApplicationSettingsBase.Save is called
        // ASB makes sure to pass each provider only the values marked for that provider -
        // though in this sample, since the entire settings class was marked with a SettingsProvider
        // attribute, all settings in that class map to this provider
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection propvals)
        {
            // Iterate through the settings to be stored
            // Only IsDirty=true properties should be included in propvals
            foreach (SettingsPropertyValue propval in propvals)
            {

                SetSettingValue(propval.Name, propval.SerializedValue);
            }
        }

        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection props)
        {

            // Create new collection of values
            SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();

            // Iterate through the settings to be retrieved
            foreach (SettingsProperty setting in props)
            {
                SettingsPropertyValue value = new SettingsPropertyValue(setting);
                value.IsDirty = false;
                value.SerializedValue = GetSettingValue(setting);
                values.Add(value);
            }
            return values;
        }

        private IsolatedStorageFile GetStore()
        {
            return IsolatedStorageFile.GetStore( IsolatedStorageScope.Machine | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain, null, null);
        }

        private object GetSettingValue(SettingsProperty setting)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            var settings = new Dictionary<string, object>();
            var store = GetStore();
            using (var stream = store.OpenFile("settings.cfg", FileMode.OpenOrCreate, FileAccess.Read))
            {
                if (stream.Length > 0) settings = (Dictionary<string, object>)formatter.Deserialize(stream);
            }
            return (settings.ContainsKey(setting.Name)) ? settings[setting.Name] : null;
        }

        private void SetSettingValue(string name, object value)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            var settings = new Dictionary<string, object>();
            var store = GetStore();
            using (var stream = store.OpenFile("settings.cfg", FileMode.OpenOrCreate, FileAccess.Read))
            {
                if (stream.Length > 0) settings = (Dictionary<string, object>)formatter.Deserialize(stream);
            }
            settings[name] = value;
            using (var stream = store.OpenFile("settings.cfg", FileMode.OpenOrCreate, FileAccess.Write))
            {
                formatter.Serialize(stream, settings);
            }
            return ;
        }

    }
}

This is how I did it. Now all I need to kn ow is if I need to implement IApplicationSettingsProvider in order to successfully migrate settings between versions? Peer reviews and comments welcome!

using System.Collections.Generic;
using System.Configuration;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.IO.IsolatedStorage;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace NetworkManager
{
    class IsolatedStorageSettingsProvider : SettingsProvider
    {
        public IsolatedStorageSettingsProvider()
        {

        }

        public override string ApplicationName
        {
            get { return Application.ProductName; }
            set { }
        }

        public override void Initialize(string name, NameValueCollection col)
        {
            base.Initialize(this.ApplicationName, col);
        }

        // SetPropertyValue is invoked when ApplicationSettingsBase.Save is called
        // ASB makes sure to pass each provider only the values marked for that provider -
        // though in this sample, since the entire settings class was marked with a SettingsProvider
        // attribute, all settings in that class map to this provider
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection propvals)
        {
            // Iterate through the settings to be stored
            // Only IsDirty=true properties should be included in propvals
            foreach (SettingsPropertyValue propval in propvals)
            {

                SetSettingValue(propval.Name, propval.SerializedValue);
            }
        }

        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection props)
        {

            // Create new collection of values
            SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();

            // Iterate through the settings to be retrieved
            foreach (SettingsProperty setting in props)
            {
                SettingsPropertyValue value = new SettingsPropertyValue(setting);
                value.IsDirty = false;
                value.SerializedValue = GetSettingValue(setting);
                values.Add(value);
            }
            return values;
        }

        private IsolatedStorageFile GetStore()
        {
            return IsolatedStorageFile.GetStore( IsolatedStorageScope.Machine | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain, null, null);
        }

        private object GetSettingValue(SettingsProperty setting)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            var settings = new Dictionary<string, object>();
            var store = GetStore();
            using (var stream = store.OpenFile("settings.cfg", FileMode.OpenOrCreate, FileAccess.Read))
            {
                if (stream.Length > 0) settings = (Dictionary<string, object>)formatter.Deserialize(stream);
            }
            return (settings.ContainsKey(setting.Name)) ? settings[setting.Name] : null;
        }

        private void SetSettingValue(string name, object value)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            var settings = new Dictionary<string, object>();
            var store = GetStore();
            using (var stream = store.OpenFile("settings.cfg", FileMode.OpenOrCreate, FileAccess.Read))
            {
                if (stream.Length > 0) settings = (Dictionary<string, object>)formatter.Deserialize(stream);
            }
            settings[name] = value;
            using (var stream = store.OpenFile("settings.cfg", FileMode.OpenOrCreate, FileAccess.Write))
            {
                formatter.Serialize(stream, settings);
            }
            return ;
        }

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