/* File Items.csc part of the serializableDemo example Mike Barnes 11/15/03 */ using System; using System.Collections; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace serializableDemo { [Serializable] class Items : ArrayList { // read all the items from open file file and place in vector items public void deSerialize (string fileName) { FileStream fin = null; // file to read // open file to read Console.WriteLine("Reading {0} from {1}", this, fileName); try { fin = new FileStream(fileName, FileMode.Open); // Console.WriteLine("read file {0}", fin); BinaryFormatter bf = new BinaryFormatter(); // Console.WriteLine(bf); this.Clear(); // clear the contents of the arrayList this.AddRange((Items) bf.Deserialize(fin)); } catch (SerializationException e) { Console.WriteLine("Failed to serialize. Reason: " + e.Message); throw; } finally { fin.Close(); } } public void serialize (string fileName) { FileStream fout = null; // file to read // open file to write Console.WriteLine("Writing {0} to {1}", this, fileName); try { fout = new FileStream(fileName, FileMode.Create); // Console.WriteLine("write file {0}", fout); BinaryFormatter bf = new BinaryFormatter(); // Console.WriteLine(bf); bf.Serialize(fout, ((Items) this)); } catch (SerializationException e) { Console.WriteLine("Failed to serialize. Reason: " + e.Message); throw; } finally { fout.Close(); } } public Items() { string str; Add(new Item("listen to Pat Metheny")); str = "Get a plan for how to complete the first project in 585. "; str += "Perhaps I can look at some examples and then mock up some stuff."; Add(new Item("585 p1", str)); Add(new Item("wash clothes")); Add(new Item("write home")); str = "Create separate subdirectories for each version of my "; str += "projects in each of the classes that I am taking."; Add(new Item("organize hard disk", str)); } public void show() { Console.WriteLine("Show {0} items", Count); Console.WriteLine(); foreach (Item token in this) { Console.WriteLine(token.ToString()); Console.WriteLine(); } } } // end class Items }