Advertisement
Guest User

SaveLoad.cs

a guest
Dec 10th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. using System;
  2. using System.Dynamic;
  3. using System.IO;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using System.Windows.Forms;
  6.  
  7. namespace Polygons
  8. {
  9.     public static class SaveLoadManager
  10.     {
  11.         private static readonly BinaryFormatter _binaryFormatter;
  12.         private static readonly SaveFileDialog _saveFileDialog;
  13.         private static readonly OpenFileDialog _openFileDialog;
  14.  
  15.         static SaveLoadManager()
  16.         {
  17.             _binaryFormatter = new BinaryFormatter();
  18.  
  19.             _saveFileDialog = new SaveFileDialog
  20.             {
  21.                 AddExtension = true,
  22.                 CheckPathExists = true,
  23.                 DefaultExt = "plg"
  24.             };
  25.  
  26.             _openFileDialog = new OpenFileDialog
  27.             {
  28.                 Filter = "plg files (*.plg)|*.plg|All files (*.*)|*.*"
  29.             };
  30.         }
  31.  
  32.         public static void Save(IMemento obj, string directory = null)
  33.         {
  34.             if (obj == null) throw new ArgumentNullException(nameof(obj));
  35.             if (directory != null) _saveFileDialog.InitialDirectory = directory;
  36.  
  37.             _saveFileDialog.FileName =
  38.                 DateTime.Now.Year + "-" +
  39.                 DateTime.Now.Month + "-" +
  40.                 DateTime.Now.Day + "_" +
  41.                 DateTime.Now.Hour + "-" +
  42.                 DateTime.Now.Minute + "_data";
  43.  
  44.             if (_saveFileDialog.ShowDialog() == DialogResult.OK)
  45.             {
  46.                 Stream SaveFileStream = File.Create(_saveFileDialog.FileName);
  47.  
  48.                 _binaryFormatter.Serialize(SaveFileStream, obj);
  49.  
  50.                 SaveFileStream.Close();
  51.             }
  52.         }
  53.  
  54.         public static IMemento Load(string directory = null)
  55.         {
  56.             if (directory != null) _openFileDialog.InitialDirectory = directory;
  57.  
  58.             IMemento memento = null;
  59.             if (_openFileDialog.ShowDialog() == DialogResult.OK && File.Exists(_openFileDialog.FileName))
  60.             {
  61.                 Stream openFileStream = File.OpenRead(_openFileDialog.FileName);
  62.                 try
  63.                 {
  64.                     memento = (IMemento)_binaryFormatter.Deserialize(openFileStream);
  65.                 }
  66.                 catch
  67.                 {
  68.                     MessageBox.Show("Unable to find a plugin!");
  69.                     memento = null;
  70.                 }
  71.                 openFileStream.Close();
  72.             }
  73.             return memento;
  74.         }
  75.     }
  76.  
  77.     public interface IOriginator
  78.     {
  79.         IMemento CreateMemento();
  80.         void SetMemento(in IMemento memento);
  81.     }
  82.  
  83.     public interface IMemento
  84.     {
  85.         ExpandoObject GetState();
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement