C# program to read text from a file

C# | Read text from a file: In this tutorial, we will learn how to read text from a file using C# program? Submitted by IncludeHelp, on October 28, 2017 [Last updated : March 23, 2023]

Reading text from a file in C#

To read text from a file in C#, we can simply use File.ReadAllText() which is a static method of File class.

Syntax:

string File.ReadAllText(path);

Where, path is the filename with it's location or file path.

C# program to read text from a file

using System;

// We need to include this namespace 
// for file handling

using System.IO;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      string s;

      s = File.ReadAllText("ABC.TXT");

      Console.WriteLine("Content of file :\n" + s);
    }
  }
}

Output

Content of file :
Hello , This is a sample program for writing text into file.

Explanation

In the above program, we need to remember, when we use "File" class, System.IO namespace must be included in the program.

C# File Handling Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.