//Сериализирует класс настроек и сохраняет его в файл internal void saveSettings(UserPrefs userPrefs) { if (userPrefs.ShouldSaveData) { // создаем объект BinaryFormatter BinaryFormatter formatter = new BinaryFormatter(); // получаем поток, куда будем записывать сериализованный объект if (!Directory.Exists(appData + "\\UTMChangerData")) { Directory.CreateDirectory(appData + "\\UTMChangerData"); } using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate)) { try { formatter.Serialize(stream, userPrefs); } catch (Exception) { // throw; } } } }
public MainWindow() { baseFunctions = new BaseFunctions(this); prefs = baseFunctions.loadSettings(); InitializeComponent(); applyPrefs(); }
//Десериализует обьект настроек и создает класс настроек internal UserPrefs loadSettings() { UserPrefs userPrefs = null; if (Directory.Exists(appData + "\\UTMChangerData")) { if (File.Exists(appData + "\\UTMChangerData\\config.dat")) { BinaryFormatter formatter = new BinaryFormatter(); using (FileStream fs = new FileStream(path, FileMode.Open)) { try { userPrefs = (UserPrefs)formatter.Deserialize(fs); } catch (Exception) { //throw; } } } else { userPrefs = new UserPrefs(); if (userPrefs.Parsers.Keys.Count == 0) { userPrefs.addParser("habr", new ParserCreator("post__title_link", "a", "https://habr.com/", "page{CurrentId}")); } } } else { userPrefs = new UserPrefs(); if (userPrefs.Parsers.Keys.Count == 0) { userPrefs.addParser("habr", new ParserCreator("post__title_link", "a", "https://habr.com/", "page{CurrentId}")); } } // Console.WriteLine("Объект десериализован"); return(userPrefs); }
public Settings(UserPrefs prefs) { this.userPrefs = prefs; InitializeComponent(); shouldSaveData.IsChecked = prefs.ShouldSaveData; }