I am writing my own settings provider:
public static class SettingsProvider
{
private const string SettingsFilePath = "Settings.xml";
private static ISettings _settings;
public static ISettings Settings
{
get
{
if (_settings == null)
{
_settings = readSettings();
}
return _settings;
}
set
{
_settings = value;
saveSettings(value);
}
}
private static ISettings readSettings()
{
if (File.Exists(SettingsFilePath))
{
using (Stream stream = File.Open(SettingsFilePath, FileMode.Open))
{
var bf = new BinaryFormatter();
return (ISettings)bf.Deserialize(stream);
}
}
return new Settings();
}
private static void saveSettings(ISettings settings)
{
using (Stream stream = File.Open(SettingsFilePath, FileMode.Create))
{
var bf = new BinaryFormatter();
bf.Serialize(stream, settings);
}
}
}
I want use it like that:
var settings = SettingsProvider.Settings; //get settings from xml
var settings2 = SettingsProvider.Settings; //second time, i dont need to look to xml again
SettingsProvider.Settings = new Settings(); //Settings() implemented ISettings
I have a problem with assignment new settings, method of property Settings is never called, so is not saved to the xml. How to do that?