我想使用.NET设置设计器/框架在设计时创建应用程序设置,并让这些设置在运行时由应用程序写入。它似乎开箱即用,我无法创建可由应用程序更改的设置,并且所有用户在运行应用程序时都会读取这些设置?
我的代码只允许所有用户使用一个应用程序实例,因此冲突不是问题。
到目前为止,我认为我需要创建一个自定义的SettingsProvider。我希望我能以某种方式继承LocalFileSettingsProvider并覆盖文件位置,但无法找到办法。
答案 0 :(得分: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 ;
}
}
}