using System; using System.IO; using System.Windows; using System.Xml.Serialization; namespace GCHR.Control.IO { abstract class ConfigIO { protected ConfigIO(string datei, Type type) { _datei = datei; _type = type; } private readonly string _datei; private readonly Type _type; private string BackupDatei { get { return string.Format("{0}.bak", _datei); } } public abstract object Laden(); protected object DateiLaden() { if (!File.Exists(_datei)) return null; var serializer = new XmlSerializer(_type); using (var stream = File.OpenText(_datei)) { var gcsConfig = serializer.Deserialize(stream); stream.Close(); return gcsConfig; } } public void Speichern(object config) { var serializer = new XmlSerializer(_type); try { using (var stream = File.CreateText(BackupDatei)) { serializer.Serialize(stream, config); stream.Close(); } File.Delete(_datei); File.Move(BackupDatei, _datei); } catch (Exception) { MessageBox.Show( string.Format("Es ist ein Fehler aufgetreten. Die Datei '{0}' konnte nicht gespeichert werden.", _datei)); } } } }