title: Develop a file reading routine in c#
body: <p>I have developed a file reading routine using c#.net that will read the entire file contents into memory using a suitable Data Class or structure.</p>
  
  <p>I have a text file of 600MB that has RoadId and many other entries. I have to read that file using query method so I used Stream Reader in c#.net that reads line by line. But I want to know is there any other method in c#.net that will be memory efficient and less time taking or by converting the text to binary and then reading. </p>
  
  <p>Not sure please guide me through this.</p>
  
  <p>I am putting my code for reading whole file line by line...</p>
  
  <pre><code>     public static void read_time()
      {
          DateTime end;
          StreamReader file =
             new StreamReader(@"C:\Users\Reva-Asus1\Desktop\DTF Test\F_Network_out.txt");
          DateTime start = DateTime.Now;
          while ((file.ReadLine()) != null) ;
  
          end = DateTime.Now;
          Console.WriteLine();
          Console.WriteLine("Full File Read Time: " + (end - start));
          Console.WriteLine();
          file.Close();
          Console.WriteLine("Data is read");
          Console.ReadLine();
          return;
      }
  </code></pre>
  
  <p>// This querying method is to take roadId from user from console and display the record....</p>
  
  <pre><code>     public static void querying_method()
      {
         Console.WriteLine("Give a RoadId to search record\n");
         DateTime start, end;
         string id =Console.ReadLine().Trim();
          try
          {
  
              System.IO.StreamReader file =
                 new System.IO.StreamReader(@"C:\Users\Reva-Asus1\Desktop\DTF Test\F_Network_out.txt");
              string line1;
              int count = 1;
              start = DateTime.Now;
              while ((line1 = file.ReadLine()) != null)
              {
                  if(line1 == id)
                  {
                      string line2 = " ";
                      while (count != 14)
                      {
                          Console.WriteLine(line2 = file.ReadLine());
                          count++;
  
                      }
                      int n = Convert.ToInt16(line2);
                      while (n != 0)
                      {
                          Console.WriteLine(line2 = file.ReadLine());
                          n--;
                      }
                      break;
                  }
              }
              end = DateTime.Now;
              Console.WriteLine("Read Time for the data record: " + (end - start));
              Console.ReadLine();
              return;
          }
          catch (Exception)
          {
              Console.WriteLine("No ID match found in the file entered by user");
              Console.ReadLine();
              return;
          }
      }
  </code></pre>