C# File ReadAllText(String, Encoding)

Description

File ReadAllText(String, Encoding) Opens a file, reads all lines of the file with the specified encoding, and then closes the file.

Syntax

File.ReadAllText(String, Encoding) has the following syntax.


public static string ReadAllText(
  string path,
  Encoding encoding
)

Parameters

File.ReadAllText(String, Encoding) has the following parameters.

  • path - The file to open for reading.
  • encoding - The encoding applied to the contents of the file.

Returns

File.ReadAllText(String, Encoding) 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.


/* ww w .j  a  va  2  s .  co m*/

using System;
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, Encoding.UTF8);

        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