Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.3/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/IO/Binary/BinarySerializer.cs @ 13398

Last change on this file since 13398 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 3.5 KB
Line 
1using System;
2using System.Diagnostics;
3using System.IO;
4using System.Runtime.Serialization;
5using System.Runtime.Serialization.Formatters.Binary;
6using System.Windows.Forms;
7namespace Netron.Diagramming.Core {
8  /// <summary>
9  /// Utility class to binary (de)serialize a diagram (from) to file
10  /// </summary>
11  public static class BinarySerializer {
12    #region Methods
13
14    /// <summary>
15    /// Binary saves the diagram
16    /// </summary>
17    /// <param name="fileName">the file-path</param>
18    /// <param name="control">The control.</param>
19    /// <returns></returns>
20    public static bool SaveAs(string fileName, DiagramControlBase control) {
21
22      FileStream fs = new FileStream(fileName, FileMode.Create);
23
24      GenericFormatter<BinaryFormatter> f = new GenericFormatter<BinaryFormatter>();
25
26      try {
27        Document document = control.Document;
28
29
30        //Warning!: cleaning up, you need to unhook all events since
31        // unserializable classes hooked to events will give problems.       
32        f.Serialize<Document>(fs, document);
33        return true;
34      }
35      catch (Exception exc) {
36        //site.OutputInfo("The graph was not saved, because some graph events were attached to non-serializable classes.\r\n This is a known issue and will be resolved in a later stadium.",OutputInfoLevels.Exception);
37        Trace.WriteLine(exc.Message, "BinarySerializer.SaveAs");
38
39        //DumpInfo();
40      }
41      finally {
42        fs.Close();
43      }
44      return false;
45    }
46    /// <summary>
47    /// Opens the binary-saved diagram
48    /// </summary>
49    /// <param name="fileName">Name of the file.</param>
50    /// <param name="control">The control.</param>
51    public static bool Open(string fileName, DiagramControlBase control) {
52      FileStream fs = null;
53
54      try {
55        fs = File.OpenRead(fileName);
56      }
57      catch (System.IO.DirectoryNotFoundException exc) {
58        System.Windows.Forms.MessageBox.Show(exc.Message);
59      }
60      catch (System.IO.FileLoadException exc) {
61        System.Windows.Forms.MessageBox.Show(exc.Message);
62      }
63      catch (System.IO.FileNotFoundException exc) {
64        System.Windows.Forms.MessageBox.Show(exc.Message);
65      }
66      catch (Exception exc) {
67        throw new Exception("Non-CLS exception caught.", exc);
68      }
69      //donnot open anything if filestream is not there
70      if (fs == null) {
71        return false;
72      }
73
74      try {
75
76        GenericFormatter<BinaryFormatter> f = new GenericFormatter<BinaryFormatter>();
77        //one-line deserialization, no bits 'nd bytes necessary....C# is amazing...
78        Document document = f.Deserialize<Document>(fs);
79
80        if (document == null)
81          throw new InconsistencyException("The deserialization return 'null'.");
82        //call the standard method at the control level to attach a new document
83        //In principle you could create a document programmatically and attach it this way as well.
84        control.AttachToDocument(document);
85        return true;
86      }
87      catch (SerializationException exc) {
88        MessageBox.Show(exc.Message);
89      }
90      catch (System.Reflection.TargetInvocationException exc) {
91        MessageBox.Show(exc.Message, "BinarySerializer.Open");
92      }
93      catch (Exception exc) {
94        MessageBox.Show(exc.Message, "BinarySerializer.Open");
95      }
96      finally {
97        if (fs != null) {
98          fs.Close();
99        }
100      }
101      return false;
102    }
103
104
105    #endregion
106  }
107}
Note: See TracBrowser for help on using the repository browser.