To create a CSV file in C#, you can use the StreamWriter
class along with a delimiter. Here's an example:
main.cs402 chars16 lines
In this example, we create a new CSV file called example.csv
and add some data to it. We use StreamWriter
to write to the file, and use file.WriteLine
to write each line of text. The first line is the header, followed by two lines of data. The delimiter used in this example is a comma.
Note that this example writes the contents of the CSV file from scratch. If you already have data that needs to be added to an existing CSV file, you would need to use a different approach such as reading the existing file into memory, adding the new data to it, and then writing it back to the file.
gistlibby LogSnag