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())); } }
private void LoadPreferences() { BinaryFormatter bf = new BinaryFormatter(); LauncherEncryption myEncryption = new LauncherEncryption(); IsolatedStorageFile inFile = IsolatedStorageFile.GetUserStoreForAssembly(); //do we have the desired locale? Locales myLocales = new Locales(CultureInfo.CurrentUICulture.Name); if (App.Current.Resources["UserPreferences"] == null) { bool blnSetDefaults = false; //here we will attempt to load the settings object from the user's profil //or make them up if nothing has been chosen yet (first run) using (IsolatedStorageFileStream inStream = new IsolatedStorageFileStream("settings.cfg", FileMode.OpenOrCreate, System.IO.FileAccess.Read, inFile)) using (BinaryReader read = new BinaryReader(inStream)) { if (read.BaseStream.Length == 0) { blnSetDefaults = true; } else { try { //read in the saved bytes byte[] arInBytes = read.ReadBytes((int)read.BaseStream.Length); //decrypt byte[] arDecrypted = myEncryption.Decrypt(arInBytes); //deserialize using (MemoryStream msIn = new MemoryStream(arDecrypted)) { theSettings = bf.Deserialize(msIn) as UserSettings; } if (theSettings == null) { //make a default one blnSetDefaults = true; } } catch (Exception ex) { string strYar = ex.Message; } } } if (blnSetDefaults) { //make a new one theSettings = new UserSettings { Locale = myLocales.CurrentLocale.Locale, Skin = "SWG_DataPad", Pallette = "Alpha Blue", GhostFont = "Aurek-Besh" }; //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())); } } //and put it in the resources App.Current.Resources.Add("UserPreferences", theSettings); } else { //collect them from the Application Resources Collection theSettings = App.Current.Resources["UserPreferences"] as UserSettings; } }