C# File ReadAllLines(String, Encoding)

Description

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

Syntax

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


public static string[] ReadAllLines(
  string path,
  Encoding encoding
)

Parameters

File.ReadAllLines(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.ReadAllLines(String, Encoding) method returns

Example

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


using System;//from  ww w  . j  a  v  a 2  s.co  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" };
        File.WriteAllLines(path, createText, Encoding.UTF8);

        string[] readText = File.ReadAllLines(path, Encoding.UTF8);
        foreach (string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}




















Home »
  C# Tutorial »
    System.IO »




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