11
Reply

Write to file

Niklas Buhu

Niklas Buhu

May 22 2010 8:32 AM
4.5k

I am having a problem writing to files.
The problem is that I do not get the same result when running on windows xp and windows server 2003 as I do when I run it on a windows server 2008 machine.
The original code was from a larger program but after having debugged this for a day I decided to make a test-program.
My objective: To read a file, after having read 200 lines write lines to new file.
My result: On windows xp and 2003 machines I get 200 lines in new file. On windows 2008 machines I get 218 lines in new file.
Below is a simple test which for me behaves differently on win xp and 2008.
What am I doing wrong?
Code:
private
void ProcessFile(string inputFileName, string outputFileName)
{
   Encoding encoding = Encoding.GetEncoding(65001);
   List<string> transactionlist = new List<string>();   
   StreamReader sr = new StreamReader(inputFileName, encoding);
   List<string> splittedFiles = new List<string>();
   int lineCount = 0;
   int maxLinesFile = 200;
   while (sr.Peek() != -1)
   {
      lineCount++;
      transactionlist.Add(sr.ReadLine());
      if (lineCount == maxLinesFile)
         break;
   }
   sr.Close();
   sr.Dispose();
   File.WriteAllLines(outputFileName, transactionlist.ToArray(), encoding);
   return;
}

Answers (11)