2012-02-12 39 views
2

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

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

到目前为止,我认为我需要创建一个自定义SettingsProvider。我希望我能以某种方式从LocalFileSettingsProvider继承并覆盖文件位置,但无法找到一种方法来执行此操作。

回答

1

这就是我做到的。现在我需要知道的是,如果我需要实现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 ; 
     } 

    } 
}