1

I've got ~500 csv files, without headers, and a legacy BASIC program where the data is imported and saved as variables:

OPEN "filename" FOR INPUT AS #1
INPUT #1, var1, var2, var3$, var4, etc

Most of the files have > 60 fields, and therefore I do not think the answer given here is applicable.

I've so far been unable to find a way to do this in C#.

The new program is a Windows Forms application, and I'm going to have classes for certain objects, and the data in the csv's relate to properties of the objects. I'm going to initialize the object using either a string depicting the file to open, or a dataset if that is the correct way to go about doing this.

Does anyone know of a way to do this in C#, either using 3rd party libraries or not?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

I recommend you to use CsvHelper or FileHelpers.

  • Example with CsvHelper

Create a class with the structure of your CSV

public class Record {
    public string Field1 {get;set;}
    public int Field2 {get;set;}
    public double Field3 {get;set;}
}

Read all records

using (var sr = new StreamReader("yourFile.csv"))
{
    var csv = new CsvReader( sr );
    var records = csv.GetRecords<Record>().ToList();
}
i.masm
  • 661
  • 6
  • 7