Tuesday, March 16, 2010

Read In Tab Delimited file into Hashtable in C#

    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;

    }

No comments: