2012-02-12 12 views
2

.NET設定デザイナ/フレームワークを使用して設計時にアプリケーション設定を作成し、実行時にこれらの設定をアプリケーションで書き込むことができます。それはボックスの外に見えますが、私はアプリケーションで変更でき、アプリケーションを実行するときにすべてのユーザーが読むことができる設定を作成できません。はユーザー間で書き込み可能な設定を共有します

すべてのユーザーにアプリケーションの1つのインスタンスしか許可しないので、競合は問題になりません。

これまでのところ、私はカスタムの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 ; 
     } 

    } 
} 
関連する問題