C# Program to read all the lines of a file at once


Use ReadAllText() method to read all the lines of a file at once.

Let’s say we have a file “hello.txt” with the following lines −

One
Two
Three

To read the above file, add the path of the file as parameter.

File.ReadAllText(myPath);

Above, myPath had the file path.

String myPath = "hello.txt";

Let us see the complete code −

Example

using System;
using System.IO;
public class Demo {
   public static void Main() {
      String myPath = "hello.txt";
      String allLines;
      allLines = File.ReadAllText(myPath);
      Console.WriteLine(allLines);
   }
}

Output

The following is the output −

One
Two
Three

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 10-Apr-2020

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements