First of all need to understand what is CSV and how to write it.
(Most of answers (all of them at the moment) do not use this requirements, that's why they all is wrong!)
- Every next string (
/r/n
) is next "table" row.
- "Table" cells is separated by some delimiter symbol.
- As delimiter can be used ANY symbol. Often this is
\t
or ,
.
- Each cell possibly can contain this delimiter symbol inside of the cell (cell must to start with double quotes symbol and to have double quote in the end in this case)
- Each cell possibly can contains
/r/n
symbols inside of the cell (cell must to start with double quotes symbol and to have double quote in the end in this case)
Some time ago I had wrote simple class for CSV read/write based on standard Microsoft.VisualBasic.FileIO
library. Using this simple class you will be able to work with CSV like with 2 dimensions array.
Simple example of using my library:
Csv csv = new Csv("\t");//delimiter symbol
csv.FileOpen("c:\\file1.csv");
var row1Cell6Value = csv.Rows[0][5];
csv.AddRow("asdf","asdffffff","5")
csv.FileSave("c:\\file2.csv");
You can find my class by the following link and investigate how it's written:
https://github.com/ukushu/DataExporter
This library code is really fast in work and source code is really short.
PS: In the same time this solution will not work for unity.
PS2: Another solution is to work with library "LINQ-to-CSV". It must also work well. But it's will be bigger.