using System; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string filename = "test.csv"; string text = System.IO.File.ReadAllText(filename, Encoding.GetEncoding(874)).Trim(); string[] rows = text.Split('\n'); Console.WriteLine(string.Format("There are {0} rows in '{1}'.", rows.Count(), filename)); Regex CSVParser = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))"); for (int r = 0; r < rows.Count(); r++) { //string[] cols = rows[r].Split(','); string[] cols = CSVParser.Split(rows[r]); for (int c = 0; c < cols.Count(); c++) { // clean up the fields (remove " and leading spaces) cols[c] = cols[c].TrimStart(' ', '"'); cols[c] = cols[c].TrimEnd('"'); Console.Write(cols[c]); } Console.WriteLine(); } } } }
Link