0

I'm trying to learn c# and have a task to do to try and learn it. Part of this task is to read a csv file to manipulate the data later.

the code I have to read the file and just print to console to test it is below

string[] lines = File.ReadAllLines(@"file path here");
foreach (string line in lines)
{
    Console.WriteLine(line);
    //results.Add(line);
}

and the contents on the csv looks like this:

CustomerId,Forename,Surname,DateOfBirth,VehicleId,RegistrationNumber,Manufacturer,Model,EngineSize,RegistationDate,InteriorColour,HasHelmetStorage,VehicleType
1,Joe,Bloggs,1983-04-08,1,BJ07 YUK,Renault,Clio,1290,2007-02-28,Cream,,Car
1,Joe,Bloggs,1983-04-08,7,BJ12 UUY,Renault,Twingo,1190,2012-03-12,Grey,,Car
2,Jane,Doe,1979-09-04,2,BT58 OKJ,Ford,Fiesta,1187,2008-10-03,Black,,Car
3,Bob,Smith,1992-10-27,3,BJ53 WYR,Peugeot,108,987,2003-07-03,Grey,,Car
4,Kate,Jones,1990-12-09,4,BT12 UJJ,Renault,Clio,1190,2012-05-03,Grey,,Car
5,Ann,Banks,1987-03-10,5,BJ16 OIU,Citroen,C3,1298,2016-02-20,Black,,Car
6,Jeff,Hope,1963-01-19,6,BT15 PLM,Renault,Megane,1380,2015-04-04,Cream,,Car
6,Jeff,Hope,1963-01-19,7,BJ55 PPP,Harley-Davidson,Street 750,750,2015-10-10,,No,Motorcycle
7,Steven,Beck,1980-06-07,,,,,,,,,

my issue is that nothing prints to the console. The application runs then closes instantly I've tried using the StreamReader read line but that would only print the first line over and over whilst using a while loop.

So my question is how do I get it to print the contents of this csv so I can then go on to manipulate the data the way I need?

Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
Jack.Wol
  • 31
  • 5

1 Answers1

0

I'm assuming you are running the program directly from Visual Studio. In that case, this is the correct behaviour.

The application will close instantly because once the loop has finished running, the program quits and therefore the console window closes.

You can add Console.ReadLine(); below the loop, that way the program will wait for your input after the loop has run, so you can see the output from the program in the console.

HaukurHaf
  • 13,522
  • 5
  • 44
  • 59