日期:2014-05-17 浏览次数:20840 次
private static Mutex mutex = new Mutex(); internal static bool Serialize(object obj, string objectName, string path) { try { mutex.WaitOne(); BinaryFormatter formatter = new BinaryFormatter(); System.IO.FileStream stream = System.IO.File.Create(path + objectName); formatter.Serialize(stream, obj); stream.Close(); mutex.ReleaseMutex(); return true; } catch (Exception e) { return false; } } internal static object Deserialize(string path) { try { if (File.Exists(path)) { System.IO.FileStream stream = System.IO.File.OpenRead(path); stream.Seek(0, System.IO.SeekOrigin.Begin); BinaryFormatter formatter = new BinaryFormatter(); object obj = (object)formatter.Deserialize(stream); stream.Flush(); stream.Close(); return obj; } return null; } catch (Exception e) { return null; } }