Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to read a CSV that the user selects from an openFileDialog (C# Windows Form), and access the values in each row. The problem I am having is how to get the contents of that CSV and do something with it. This is a pretty straightforward process in PowerShell (import-csv, then use a foreach loop to process each row). Can anybody give me some direction on this?

There are a lot of posts surrounding CSV's in C# but none of them seem to do quite what I need and they all do it differently.

Thanks!
Posted
Comments
StM0n 9-Oct-15 0:16am    
I'm not quite clear of your question... do you want something like PowerShells <<import-csv>>?
Ralf Meier 9-Oct-15 0:23am    
Do you know the Content-structure of your CSV-File ?
At which Point do you stuck ?
VR Karthikeyan 9-Oct-15 0:38am    

1 solution

A critical question is whether the CSV files you want to read always have the same number of columns. If the structure of the files is always the same, you can use Linq to parse the file into a collection of IEnumerables that will correspond to Rows.

Or, you can create a pre-defined "schema" Class to parse/map the data into.

If the structure of each file varies, then you probably want results in a DataTable ... for that there is a well-tuned old library, FileHelpers, that will "walk the dog" for you: [^].

See this for an example of using FileHelpers: [^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900