Hashtable reading text file???
-
I've searched all my books and the Internet and cannot find a good example to read a text file into a hashtable. My text file is tab delimited as such below)and I need to load them into a hashtable as a DNS cashe...... Help......... 123.123.123.123 www.mywebsite.com 111.222.333.444 www.nowebsite.com
-
I've searched all my books and the Internet and cannot find a good example to read a text file into a hashtable. My text file is tab delimited as such below)and I need to load them into a hashtable as a DNS cashe...... Help......... 123.123.123.123 www.mywebsite.com 111.222.333.444 www.nowebsite.com
kvnsdr wrote: I've searched all my books and the Internet and cannot find a good example to read a text file into a hashtable. Books typically cover how to use particular mechanisms, you simply need to put the pieces together. This is a quick example method I wrote to read your file and insert the data into the
Hashtable
. Hope this helps.private Hashtable GetHashtableFromFile(string file) { Hashtable ht = new Hashtable(); string line = string.Empty; using(StreamReader reader = new StreamReader(file)) { while((line = reader.ReadLine()) != null) { string\[\] args = line.Split(new char\[\]{' '}); ht.Add(args\[0\], args\[1\]); Console.WriteLine("IP:{0}, Site:{1}", args\[0\], args\[1\]); } } return ht; }
- Nick Parker
My Blog | My Articles -
kvnsdr wrote: I've searched all my books and the Internet and cannot find a good example to read a text file into a hashtable. Books typically cover how to use particular mechanisms, you simply need to put the pieces together. This is a quick example method I wrote to read your file and insert the data into the
Hashtable
. Hope this helps.private Hashtable GetHashtableFromFile(string file) { Hashtable ht = new Hashtable(); string line = string.Empty; using(StreamReader reader = new StreamReader(file)) { while((line = reader.ReadLine()) != null) { string\[\] args = line.Split(new char\[\]{' '}); ht.Add(args\[0\], args\[1\]); Console.WriteLine("IP:{0}, Site:{1}", args\[0\], args\[1\]); } } return ht; }
- Nick Parker
My Blog | My Articles -
I like the coding. I have an error in my code that says: "Index was outside the bounds of the array" All I changed was using(StreamReader reader = new StreamReader(MyFile.txt")); ????????????????
I think you have incorrect format of input file. Somewhere there are empty strings or parts of string are not delimited with tab symbol modify the code like this private Hashtable GetHashtableFromFile(string file) { Hashtable ht = new Hashtable(); string line = string.Empty; using(StreamReader reader = new StreamReader(file)) { int i = 0; while((line = reader.ReadLine()) != null) { i++; string[] args = line.Split(new char[]{' '}); if (args.Length != 2) { Console.WriteLine("Invalid input string: " + i.ToString()); continue; } ht.Add(args[0], args[1]); Console.WriteLine("IP:{0}, Site:{1}", args[0], args[1]); } } return ht; } Invalid string will be skiped and appropriate message displayed