//public static async Task<bool> Load(this UISettings ui, string filename = "LastSaved.xsetting") //{ // ui.ShowBusySign("Loading Settings..."); // bool result = await Task<bool>.Factory.StartNew(() => { // try { // var deserializer = new XmlSerializer(typeof(UISettings)); // using (var reader = new StreamReader(filename)) { // object obj = deserializer.Deserialize(reader); // ui.UpdateAll((UISettings)obj); // } // if (MessageBox.Show($"Yes: Update the project name to the current date. => {DateTime.Now:yyyy-MM-dd}\nNo: Use the previouly saved. => {ui.ProjectName}", "Question", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No)==MessageBoxResult.Yes) { // ui.ProjectName=$"{DateTime.Now:yyyy-MM-dd}"; // } // return true; // } catch { MessageBox.Show("Failed to load the selected settings!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return false; } // }); // ui.StopBusySign(); return result; //} public static async Task <bool> LoadCrypt(this UISettings ui, string filename = "LastSaved.xsetting") { ui.ShowBusySign("Loading Settings..."); try { await Task.Factory.StartNew(() => { var deserializer = new XmlSerializer(typeof(UISettings)); using (var fs = new FileStream(filename, FileMode.Open)) { using (var sr = new StreamReader(fs)) { var aUE = new UnicodeEncoding(); byte[] key = aUE.GetBytes("password"); using (var RMCrypto = new RijndaelManaged()) { using (var cs = new CryptoStream(fs, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read)) { object obj = deserializer.Deserialize(cs); ui.UpdateAll((UISettings)obj); } } } } }); if (Path.GetFileNameWithoutExtension(filename).Substring(0, 1) == "~") { ui.WorkDirectory = Directory.GetCurrentDirectory() + "\\" + Path.GetFileNameWithoutExtension(filename).Substring(1); ui.ProjectName = $"{DateTime.Now:yyyy-MM-dd}"; } else if ((ui.ProjectName != $"{DateTime.Now:yyyy-MM-dd}") && (MessageBox.Show($"Yes: Update the project name to the current date. => {DateTime.Now:yyyy-MM-dd}\nNo: Use the previouly saved. => {ui.ProjectName}", "Would you like to update project name to the current date?", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)) { ui.ProjectName = $"{DateTime.Now:yyyy-MM-dd}"; } return(true); } catch { MessageBox.Show("Failed to load the selected settings!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return(false); } finally { ui.StopBusySign(); } }
//public static async Task<bool> Save(this DockingManager dockingManager, UISettings ui, string filename = "LastSaved.xlayout") //{ // ui.ShowBusySign("Saving Settings..."); // try { // var serializer = new XmlLayoutSerializer(dockingManager); // using (var stream = new StreamWriter(filename)) { serializer.Serialize(stream); } // ui.StopBusySign(); return true; // } catch { ui.StopBusySign(); MessageBox.Show("Failed to save the current layout!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return false; } //} public static async Task <bool> SaveCrypt(this DockingManager dockingManager, UISettings ui, string filename = "LastSaved.xlayout") { ui.ShowBusySign("Saving Settings..."); try { var aUE = new UnicodeEncoding(); byte[] key = aUE.GetBytes("password"); var RMCrypto = new RijndaelManaged(); using (var fs = File.Open(filename, FileMode.Create)) { using (var cs = new CryptoStream(fs, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write)) { var serializer = new XmlLayoutSerializer(dockingManager); serializer.Serialize(cs); } } return(true); } catch { MessageBox.Show("Failed to save the current layout!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return(false); } finally { ui.StopBusySign(); } }
//public static T DeepCopy<T>(T other) //{ // using (var ms = new MemoryStream()) { // var formatter = new BinaryFormatter(); // formatter.Serialize(ms, other); // ms.Position=0; // return (T)formatter.Deserialize(ms); // } //} //public static async Task<bool> Save(this UISettings ui, string filename = "LastSaved.xsetting") //{ // ui.ShowBusySign("Saving Settings..."); // bool result = await Task<bool>.Factory.StartNew(() => { // try { // using (FileStream fsUserSetting = File.Create(filename)) { // var formatter = new XmlSerializer(ui.GetType()); // formatter.Serialize(fsUserSetting, ui); // } // return true; // } catch { MessageBox.Show("Failed to save the current settings!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return false; } // }); // ui.StopBusySign(); return result; //} public static async Task <bool> SaveCrypt(this UISettings ui, string filename = "LastSaved.xsetting") { ui.ShowBusySign("Saving Settings..."); try { await Task.Factory.StartNew(() => { var aUE = new UnicodeEncoding(); byte[] key = aUE.GetBytes("password"); var RMCrypto = new RijndaelManaged(); using (var fs = File.Open(filename, FileMode.Create)) { using (var cs = new CryptoStream(fs, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write)) { var xml = new XmlSerializer(ui.GetType()); xml.Serialize(cs, ui); } } }); return(true); } catch { MessageBox.Show("Failed to save the current settings!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return(false); } finally { ui.StopBusySign(); } }