C# File ReadAllText(String)

Description

File ReadAllText(String) Opens a text file, reads all lines of the file, and then closes the file.

Syntax

File.ReadAllText(String) has the following syntax.


public static string ReadAllText(
  string path
)

Parameters

File.ReadAllText(String) has the following parameters.

  • path - The file to open for reading.

Returns

File.ReadAllText(String) method returns A string containing all lines of the file.

Example

The following code example demonstrates the use of the ReadAllText method to display the contents of a file.


using System;/*w  ww  .  j av  a 2  s. c  o  m*/
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        string createText = "Hello and java2s.com" + Environment.NewLine;
        File.WriteAllText(path, createText);

        string readText = File.ReadAllText(path);
        Console.WriteLine(readText);
    }
}




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter