Appending to the beginning of a text file

by David Kiff 23. December 2006 04:39

To add a header row to a CSV file I needed to append to the beginning of the text file, this however is not possible, instead I had to create a new file and write the original files contents to it after the header has been written:

StreamReader reader = new StreamReader(File.Open("Original.csv", FileMode.Open, FileAccess.Read));
StreamWriter writer = new StreamWriter(File.Create("Newfile.csv"));
writer.WriteLine("StudentID, Username, Forename, Surname"); //Write header row.
writer.WriteLine(reader.ReadToEnd()); //Write original file to the new one.
writer.Flush();
writer.Close();
reader.Close();

Tags:

Comments

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading