source: trunk/CrypPlugins/WorkspaceManager/Model/ModelPersistance.cs @ 1681

Last change on this file since 1681 was 1681, checked in by kopal, 12 years ago

some bug fixes

File size: 2.7 KB
Line 
1/*                             
2   Copyright 2010 Nils Kopal, Viktor M.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21using System.IO;
22using System.Xml.Serialization;
23using System.Runtime.Serialization.Formatters.Binary;
24
25namespace WorkspaceManager.Model
26{
27    /// <summary>
28    /// Class with static methods for loading and saving of WorkspaceModels
29    /// </summary>
30    public class ModelPersistance
31    {
32        /// <summary>
33        /// Deserializes a model from the given file with the given filename
34        /// </summary>
35        /// <param name="filename"></param>
36        /// <returns></returns>
37        public static WorkspaceModel loadModel(string filename)
38        {
39            FileStream fileStream = null;
40
41            try
42            {
43                fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
44                BinaryFormatter binaryFormatter = new BinaryFormatter();             
45                return (WorkspaceModel)binaryFormatter.Deserialize(fileStream);
46            }
47            finally
48            {
49                if (fileStream != null)
50                {
51                    fileStream.Close();
52                }
53            }
54        }
55
56        /// <summary>
57        /// Serializes the given model to a file with the given filename
58        /// </summary>
59        /// <param name="workspaceModel"></param>
60        /// <param name="filename"></param>
61        /// <returns></returns>
62        public static void saveModel(WorkspaceModel workspaceModel, string filename)
63        {
64            FileStream fileStream = null;
65
66            try
67            {
68                fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write);
69                BinaryFormatter binaryFormatter = new BinaryFormatter();
70                binaryFormatter.Serialize(fileStream, workspaceModel);               
71            }               
72            finally
73            {
74                if (fileStream != null)
75                {
76                    fileStream.Flush();
77                    fileStream.Close();
78                }
79            }
80        }
81    }
82}
Note: See TracBrowser for help on using the repository browser.