Q:

C# program to read text from a file

0

Given text, and we have to read text from the file using C# program.

File.ReadAllText()

This is a static method of "File" class, which is used to read all text from a file.

Syntax:

string File.ReadAllText(path);

Parameters:

  1. path - Filename with its location.

Return Value:

  1. string - Returned string that contains text which is written into text file..
  2.  

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

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.

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# file handling (File class) solved programs/examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to read text from a file and append in ... >>
<< C# program to write text to a file...