Shabat Closer

Sunday, March 31, 2013

c#: save and open hashtable to file.

Save Hashtable object in file:


static void SaveHashtableFile(Hashtable ht,string path){
            BinaryFormatter bfw = new BinaryFormatter();
            FileStream file = File.OpenWrite(path);
            StreamWriter ws = new StreamWriter(file);
            bfw.Serialize(ws.BaseStream, ht);
            file.Close();
        }


Open the save Hashtable file



static Hashtable OpenHashtableFile(string path) {
            FileStream filer = File.OpenRead(path);
            StreamReader readMap = new StreamReader(filer);
            BinaryFormatter bf = new BinaryFormatter();
            Hashtable ret = (Hashtable)bf.Deserialize(readMap.BaseStream);
            file.Close();
            return ret;
        }
usage:

//Open Hash table file
Hashtable ht=OpenHashtableFile("ha.dat");

//Save Hash table into file


SaveHashtableFile(ht,"ha.dat");