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

CsharpProgrammingServer Side Programming

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
raja
Published on 11-Sep-2018 13:00:43
Advertisements