C# StreamWriter: Writing Data to CSV File

  • Context: C# 
  • Thread starter Thread starter kiwijoey
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 4K views
kiwijoey
Messages
2
Reaction score
0
Can someone please, using the following data, show me how to write this data to a CSV file using the StreamWriter class in C#.

Bob,1,2,3
Jim,4,5,6
Tom,7,8,9
 
Physics news on Phys.org
using System.IO; // file readers and writers

string myInputLine = "some line to be written to a file";

string fileSpecification = "C:\test1.txt";

StreamWriter filewriter = new StreamWriter(
fileSpecification, true, System.Text.Encoding.UTF8);

filewriter.WriteLine(myInputLine);
filewriter.Flush();

filewriter.Close();