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?

2
  • Even in VB6 INPUT wasn't used. The scripting runtime allowed reading, writing files in a humane way. Commented Jun 9, 2017 at 11:09
  • The duplicate applies. It doesn't matter if you have 5 or 60 columns, a text file is a text file. There are many CSV parsing libraries too. Commented Jun 9, 2017 at 11:12

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.