Read each line in file and put each line into a string

I have a text file that I want to read in and put each line from the file into its own string.
So the file will have 4 lines:

2017-01-20
05:59:30
+353879833382
971575 Michael

So in the code I need to read in the file and split up each line and put them into a string i.e the first line will equal to string date, second line will equal to string time etc

Code:

public static void ParseTXTFile(string FileName, int CompanyID)
        {
            try
            {
                FileInfo file = new FileInfo(FileName);
                string Date;
                string Time;
                string Phone;
                string JobNo;
                string Name;

                using (CsvReader reader = new CsvReader(new StreamReader(FileName), false))
                {
                    while (reader.ReadNextRecord())
                    {


                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

How do I read in each line of the file and set it to a string?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You may want to consider using the File.ReadAllLines() method which will store each line of your file into an array :

var lines = File.ReadAllLines(FileName);

You could then access each of your properties by their indices as needed :

string Date = lines[0];
string Time = lines[1];
string Phone = lines[2];
string JobNo = lines[3];
string Name = lines[4];


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x