title: Extract specific lines of text from a text file c#
body: <p>I am reading in a text file line by line using StreamReader and displaying it to the console. It reads in and displays just fine. Each line contains from left to right, Date, Time, Number, Entry. The text file has hundreds of lines of text. I want to allow a user to enter a specific Date and Time and get back only the lines of text within that time frame. So, out of hundreds of lines of text, they can be returned only the lines of text for their inputted Date and Time.</p>
  
  <p>Here's my code so far to read in and display the text file.</p>
  
  <pre><code>    public override void ReadFile(string strFileName)
      {
          try
          {
              using (StreamReader sr = new StreamReader(@"C:\MyFolder\TextFile.txt"))
              {
                  String line = sr.ReadLine();
                  Console.WriteLine(line);
              }
          }
          catch (Exception e)
          {
              Console.WriteLine("The file could not be read:");
              Console.WriteLine(e.Message);
          }
      }
  </code></pre>
  
  <p>This works and displays text on the console like the following:</p>
  
  <pre><code>01-01-2015 10:10:10 Line of text blah blah blah
  01-01-2015 10:10:10 Line of text blah blah blah
  01-01-2015 10:10:10 Line of text blah blah blah
  </code></pre>
  
  <p>I am now trying to separate the lines to get the individual parts like date and time. This is what I have so far but I'm not sure what's the best way to proceed.</p>
  
  <pre><code>        public override void ReadLiFile(string strFileName)
      {
          try
          {
              using (StreamReader sr = new StreamReader(@"C:\MyFolder\TextFile.txt"))
              {
                  string line;
                  while ((line = sr.ReadLine()) != null)
                  {
                      string[] fields = line.Split("\t".ToCharArray()); 
                      int theInt = Convert.ToInt32(fields[0]);//to get first field e.g Date
                  }
                  Console.WriteLine(line);
              }
              }
          }
          catch (Exception e)
          {
              Console.WriteLine("The file could not be read:");
              Console.WriteLine(e.Message);
          }
      }
  </code></pre>
  
  <p>I want to narrow down what's output to screen based on a given date and time. I don't want all the lines to appear just the lines relevant to a certain date and time say for example lines between
      01-01-2015 10:10:10 and
      01-01-2015 10:15:15</p>
  
  <p>I'm guessing I need to isolate the date and time fields in each line and store the value. I have tried to split the line and store the first value which is Date. <strong>My question is what's the best way to split the fields in the line?</strong> My attempt is not working. I'm now getting back an error saying the 'File cannot be read' and also 'Input string is not in the correct format'
  Thank you for any help.</p>