title: Cannot read from a closed TextReader
body: <p>I've got a system that processes some large CSV files. </p>
  
  <p>The scenario has now arisen where these files might have a number of none-delimited, worthless lines preceding the actual comma-delimited content.</p>
  
  <p>The approach I have taken is to create a temporary reader to ascertain the number of superfluous lines and then move the working TextReader on that number of lines ready to be processed.</p>
  
  <p>My code is as follows:</p>
  
  <pre><code>private static TextReader PrepareReader(TextReader reader)
      {
          // Variables
          TextReader tmpReader = reader;
          Int32 superfluousLineCount = 0;
  
          // Determine how many useless lines we have
          using (tmpReader)
          {
              string line;
              string headerIdentifier = "&amp;1,";
              while ((line = tmpReader.ReadLine()) != null)
              {
                  // Check if the line starts with the header row identifier
                  if (line.Substring(0, 3) != headerIdentifier)
                  {
                      // Increment the superfluous line counter
                      superfluousLineCount++;
                  }
                  else
                  {
                      break;
                  }
              }
          }
  
          // Move the source reader through how many lines we want to ignore
          using (reader)
          {
              for (int i = superfluousLineCount; i &gt; 0; i--)
              {
                  reader.ReadLine();
              }
          }
  
          // Return
          return reader;
      }
  </code></pre>
  
  <p>However, the <code>reader.ReadLine();</code> in this part of the code:</p>
  
  <pre><code>for (int i = superfluousLineCount; i &gt; 0; i--)
  {
  reader.ReadLine();
  }
  </code></pre>
  
  <p>...throws the following exception</p>
  
  <blockquote>
    <p>Cannot read from a closed TextReader.
    ObjectDisposedException in mscorlib
    Method:
      Void ReaderClosed()</p>
    
    <p>Stack Trace:
    at System.IO.__Error.ReaderClosed()
    at System.IO.StreamReader.ReadLine()
    at CsvReader.PrepareReader(TextReader reader) in CsvReader.cs:line 93</p>
  </blockquote>
  
  <p>Any advice greatly appreciated.  Also, is the best way to go about my challenge?</p>
  
  <p>Notes: Framework 2.0</p>
  
  <p>Thanks.</p>