public NameServer() { sr = new serialization(); try { fileTree = sr.deserialize(); } catch (Exception e) { Console.WriteLine("Can't deserialize file tree!"); Console.WriteLine(e.Message); throw new Exception("Can't deserialize!"); } try { listOfFileServers = sr.readFileServersList(); } catch (Exception e) { Console.WriteLine("Can't read file servers list!"); Console.WriteLine(e.Message); throw new Exception("Can't read list of file servers!"); } }
public FileTree deserialize() { FileTree fileTree; FileStream fs; BinaryFormatter bf = new BinaryFormatter(); if (File.Exists("D:\\nameServer\\fileTree.dat")) { try { fs = new FileStream("D:\\nameServer\\fileTree.dat", FileMode.Open); } catch (Exception e) { Console.WriteLine("Can't open existing file with file system tree!"); Console.WriteLine(e.Message); return null; } fileTree = (FileTree)bf.Deserialize(fs); fs.Close(); } else { try { fs = new FileStream("D:\\nameServer\\fileTree.dat", FileMode.Create); } catch (Exception e) { Console.WriteLine("Can't create file with file system tree!"); Console.WriteLine(e.Message); return null; } fileTree = new FileTree(); bf.Serialize(fs, fileTree); fs.Close(); } return fileTree; }
public bool serialize(FileTree tree) { try { FileStream fs = new FileStream("D:\\nameServer\\fileTree.dat", FileMode.Create); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, tree); fs.Close(); return true; } catch (Exception e) { Console.WriteLine("Can't serialize file!"); Console.WriteLine(e.Message); return false; } }