/// <summary> /// Чтение настроек /// </summary> public SettingsFields ReadSettings(SettingsFields settFields) { var fd = new OpenFileDialog(); fd.Filter = "Файлы настроек|*.xml"; var result = fd.ShowDialog(); if (result == true) { var ser = new XmlSerializer(typeof(SettingsFields)); using (var reader = new FileStream(fd.FileName, FileMode.Open)) { try { settFields = ser.Deserialize(reader) as SettingsFields; } catch { throw new IOException { Source = fd.FileName }; } } } return(settFields); }
/// <summary> /// Запись настроек /// </summary> public SettingsFields WriteSettings(SettingsFields settFields, string path = null) { var ser = new XmlSerializer(typeof(SettingsFields)); using (var writer = File.Create(path ?? settingsLocation)) { ser.Serialize(writer, settFields); } return(settFields); }
/// <summary> /// Сохранение настроек /// (по нажатию кнопки) /// </summary> private void SaveSettings() { GetSettingsFromUI(); var fd = new SaveFileDialog(); fd.FileName = "settings"; fd.DefaultExt = ".xml"; var result = fd.ShowDialog(); if (result == true) { var newSettings = settFields.Clone(); settFields = settingsSaver.WriteSettings(newSettings, fd.FileName); } }
/// <summary> /// Конструктор /// </summary> public MainWindow() { InitializeComponent(); settingsSaver = new SettingsSaver(); Stop.IsEnabled = false; SuperInter.Visibility = Visibility.Hidden; SuperOptions.Visibility = Visibility.Hidden; myCanvas.Background = new SolidColorBrush(Color.FromArgb(0, 0, 150, 0)); settFields = new SettingsFields(); Closing += (sender, e) => SilentSaveSettings(); var settFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.xml"); if (!File.Exists(settFilePath)) { SilentSaveSettings(); } else { Task.Run(async() => { await Task.Delay(500); Application.Current?.Dispatcher?.BeginInvoke(new Action(() => { try { SilentReadSettings(); } catch (IOException exception) { MessageBox.Show($"Не удалось загрузить сохраненные настройки приложения {Environment.NewLine}Файл: {Path.GetFileName(exception.Source)}"); } })); }); } }
/// <summary> /// Восстановление настроек /// </summary> private void SilentReadSettings() { settFields = settingsSaver.SilentReadSettings(); SetSettingsToUI(); }
/// <summary> /// Чтение настроек /// </summary> private void ReadSettings() { settFields = settingsSaver.ReadSettings(settFields); SetSettingsToUI(); }