Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a delimited text file by line and by delimiter in C#

Tags:

c#

loops

I would just like to apologise for mposting this, there are a lot of questions like this here but I can't find one that is specific to this.

I have a list and each item contains a DateTime, int and a string. I have successfully written all list items to a .txt file which is delimited by commas. For example 09/04/2015 22:12:00,10,Posting on Stackoverflow.

I need to loop through the file line by line, each line starting at index 0, through to index 2. At the moment I am able to call index 03, which returns the DateTime of the second list item in the text file. The file is written line by line, but I am struggling to read it back with the delimiters and line breaks.

I am sorry if I am not making much sense, I will appreciate any help, thank you.

like image 776
Jack Avatar asked Dec 03 '22 17:12

Jack


1 Answers

string[] lines = File.ReadAllLines( filename );
foreach ( string line in lines )
{
  string[] col = line.Split(',');
  // process col[0], col[1], col[2]
}
like image 83
i486 Avatar answered Feb 16 '23 02:02

i486