Thursday, September 13, 2018

Read through a flat file one line at a time in C#.

string line;
using (var streamReader = new StreamReader(initialDetails.FromPath))
{
   while ((line = streamReader.ReadLine()) != null)
   {

 
 

I bit into this yesterday in the name of chunking a large file out to smaller ones. I learned the hard way that you should pull all of the lines out to a list of strings and THEN afterward attempt the writing. Do not try to write to other files while reading/looping. The while loop misbehaves.

No comments:

Post a Comment