Вот как я это сделал.Теперь все, что мне нужно знать, - это если мне нужно реализовать 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 ;
}
}
}