title: Streamwriter only writes a certain amount of bytes from a string list
body: <p>I have 27 csv files containing employee timesheet data with about 2000 lines of data in total, and is growing day by day.</p>
  
  <p>I am writing an application that compiles the data in each file to a single csv file for analysis in Excel. I do this by reading each file, write its contents to a string list and then write the string list to a new file. Below is the code writing the list to the file:</p>
  
  <pre><code>FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.CreateNew, FileAccess.ReadWrite);
  StreamWriter sw = new StreamWriter(fs);
  foreach (string l in reader)//reader is a List&lt;string&gt; pouplated earlier
  {
      sw.WriteLine(l);
  }
  fs.Close();
  </code></pre>
  
  <p><code>reader</code> is a list containing about 1170 lines of timesheet entries. When I query the contentents of <code>reader</code> all of the data is present.</p>
  
  <p>The problem comes when I open the compiled csv, all of the data is present except for the last few lines of the last timesheet file. The compiled file contains only 1167 full lines with line 1168 cut off at some arbitrary point. After scrutinizing different files with different contents I noticed that each file is exactly 160840bytes in size.</p>
  
  <p>For debugging purposes I modified the code like this:</p>
  
  <pre><code>string line = "";//A string to test whether the foreach loop completes
  FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.CreateNew, FileAccess.ReadWrite);
  StreamWriter sw = new StreamWriter(fs);
  foreach (string l in reader)//reader is a List&lt;string&gt; pouplated earlier
  {
      line = l;
      sw.WriteLine(line);
      sw.WriteLine("testline")//write random content to see if it has effect on the data
  }
  fs.Close();
  MessageBox.Show(line);
  </code></pre>
  
  <p>With this modification the message that pops up after the code is run displays the correct entry that should have been written to the file (but is not); this is evidence that the foreach loop loops through all the data. The compiled file now has a larger size of around 200kB. When I scrutinize it, all of the <code>"testline"</code> lines are added, the actual timesheet entry is still cut off at the same arbitrary point as when the <code>"testline"</code> lines were not written.</p>
  
  <p>In summary:</p>
  
  <ul>
  <li>I'm trying write a string list to a file</li>
  <li>All of the necessary data is present in the list</li>
  <li>The code loops through all of the data in the list</li>
  <li>The final written file does not contain all of the data in the list</li>
  </ul>
  
  <p>Can anyone advise me on what is going wrong? I will try a different approach to write the contents to the file, but I would still like to know what happens here and what I can do to prevent it.</p>