×

C# Tutorial

Basics of .Net

C# Basics

C# Fundamentals

C# Operators

C# Loops

C# Type Conversions

C# Exception Handling

C# String Class

C# Arrays

C# List

C# Stack

C# Queue

C# Collections

C# Character Class

C# Class and Object

C# Namespaces

C# Delegates

C# Constructors

C# Inheritance

C# Operator Overloading

C# Structures

C# File Handling

C# Convert.ToInt32()

C# Int32 (int) Struct

C# DateTime Class

C# Uri Class

C# Database Connectivity

C# Windows

C# Other Topics

C# Q & A

C# Programs

C# Find O/P

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 »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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