Home
Manage Your Code
Snippet: Parse CSV file to list (C#)
Title: Parse CSV file to list Language: C#
Description: Parse a delimited text file into a list Views: 282
Author: Mark Henderson Date Added: 5/5/2008
Copy Code  
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8using System.IO; //System.IO is not used by default

9
10public List<string[]> parseCSV(string path)
11{
12  List<string[]> parsedData = new List<string[]>();
13
14  try
15  {
16    using (StreamReader readFile = new StreamReader(path))
17    {
18      string line;
19      string[] row;
20
21      while ((line = readFile.ReadLine()) != null)
22      {
23        row = line.Split(',');
24        parsedData.Add(row);
25      }
26    }
27  }
28  catch (Exception e)
29  {
30    MessageBox.Show(e.Message);
31  }
32
33  return parsedData;
34}
Notes
http://blog.paranoidferret.com/index.php/2008/04/17/building-a-simple-csv-parser-in-csharp/