public void UpdateSettings(SettingsType theType, object theSetting)
{
//collect them from the Application Resources Collection
theSettings = App.Current.Resources["UserPreferences"] as UserSettings;
switch (theType)
{
case SettingsType.AddCustomServer:
case SettingsType.RemoveCustomServer:
//don't all any duplicates
theSettings.CustomServers.RemoveAll(cs => cs.ServerId == ((CustomServer)theSetting).ServerId);
if (theType == SettingsType.AddCustomServer)
{
theSettings.CustomServers.Add(theSetting as CustomServer);
}
break;
case SettingsType.AddServerCredentials:
case SettingsType.RemoveServerCredentials:
//get a serverdetail object
ServerDetails addMe = theSetting as ServerDetails;
//don't allow duplicate username for this server
theSettings.Servers.RemoveAll(sd => sd.ServerId == addMe.ServerId && sd.Username.Equals(addMe.Username, StringComparison.InvariantCultureIgnoreCase));
if (theType == SettingsType.AddServerCredentials)
{
theSettings.Servers.Add(addMe);
}
break;
default:
//update the setting
theSettings.GetType().GetProperty(theType.ToString()).SetValue(theSettings, theSetting, null);
break;
}
App.Current.Resources["UserPreferences"] = theSettings;
BinaryFormatter bf = new BinaryFormatter();
LauncherEncryption myEncryption = new LauncherEncryption();
IsolatedStorageFile inFile = IsolatedStorageFile.GetUserStoreForAssembly();
//and save that into the file
using (MemoryStream msOut = new MemoryStream())
using (IsolatedStorageFileStream outStream = new IsolatedStorageFileStream("settings.cfg", FileMode.OpenOrCreate, System.IO.FileAccess.Write, inFile))
using (BinaryWriter bwOut = new BinaryWriter(outStream))
{
//serialize it
bf.Serialize(msOut, theSettings);
//encrypt it and write it out
bwOut.Write(myEncryption.Encrypt(msOut.ToArray()));
}
}