Changeset 4068 for trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/IO/Binary
- Timestamp:
- 07/22/10 00:44:01 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/IO/Binary/BinarySerializer.cs
r3038 r4068 4 4 using System.Runtime.Serialization; 5 5 using System.Runtime.Serialization.Formatters.Binary; 6 using System.Xml.Serialization;7 using System.Xml;8 6 using System.Windows.Forms; 9 namespace Netron.Diagramming.Core 10 { 11 /// <summary> 12 /// Utility class to binary (de)serialize a diagram (from) to file 13 /// </summary> 14 public static class BinarySerializer 15 { 16 #region Methods 7 namespace 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 17 13 18 /// <summary> 19 /// Binary saves the diagram 20 /// </summary> 21 /// <param name="fileName">the file-path</param> 22 /// <param name="control">The control.</param> 23 /// <returns></returns> 24 public static bool SaveAs(string fileName, DiagramControlBase control) 25 { 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) { 26 21 27 22 FileStream fs = new FileStream(fileName, FileMode.Create); 28 23 29 24 GenericFormatter<BinaryFormatter> f = new GenericFormatter<BinaryFormatter>(); 30 25 31 26 try { … … 44 39 //DumpInfo(); 45 40 } 46 finally 47 { 48 fs.Close(); 49 } 50 return false; 51 } 52 /// <summary> 53 /// Opens the binary-saved diagram 54 /// </summary> 55 /// <param name="fileName">Name of the file.</param> 56 /// <param name="control">The control.</param> 57 public static bool Open (string fileName, DiagramControlBase control) 58 { 59 FileStream fs=null; 60 61 try 62 { 63 fs= File.OpenRead(fileName); 64 } 65 catch (System.IO.DirectoryNotFoundException exc) 66 { 67 System.Windows.Forms.MessageBox.Show(exc.Message); 68 } 69 catch(System.IO.FileLoadException exc) 70 { 71 System.Windows.Forms.MessageBox.Show(exc.Message); 72 } 73 catch (System.IO.FileNotFoundException exc) 74 { 75 System.Windows.Forms.MessageBox.Show(exc.Message); 76 } 77 catch(Exception exc) 78 { 79 throw new Exception("Non-CLS exception caught.", exc); 80 } 81 //donnot open anything if filestream is not there 82 if (fs==null) 83 { 84 return false; 85 } 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; 86 53 87 try 88 { 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 } 89 73 90 GenericFormatter<BinaryFormatter> f = new GenericFormatter<BinaryFormatter>(); 91 //one-line deserialization, no bits 'nd bytes necessary....C# is amazing... 92 Document document = f.Deserialize<Document>(fs); 74 try { 93 75 94 if(document == null) 95 throw new InconsistencyException("The deserialization return 'null'."); 96 //call the standard method at the control level to attach a new document 97 //In principle you could create a document programmatically and attach it this way as well. 98 control.AttachToDocument(document); 99 return true; 100 } 101 catch(SerializationException exc) 102 { 103 MessageBox.Show(exc.Message); 104 } 105 catch(System.Reflection.TargetInvocationException exc) 106 { 107 MessageBox.Show(exc.Message, "BinarySerializer.Open"); 108 } 109 catch(Exception exc) 110 { 111 MessageBox.Show(exc.Message, "BinarySerializer.Open"); 112 } 113 finally 114 { 115 if (fs != null) 116 { 117 fs.Close(); 118 } 119 } 120 return false; 121 } 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); 122 79 123 124 #endregion 125 } 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 } 126 107 }
Note: See TracChangeset
for help on using the changeset viewer.