C# File ReadAllLines(String)

Description

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

Syntax

File.ReadAllLines(String) has the following syntax.


public static string[] ReadAllLines(
  string path
)

Parameters

File.ReadAllLines(String) has the following parameters.

  • path - The file to open for reading.

Returns

File.ReadAllLines(String) method returns

Example

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


using System;//from  w  ww.j a  v a2 s.c  om
using System.IO;
class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        string[] createText = { "Hello", "And", "java2s.com" };
        File.WriteAllLines(path, createText);

        string[] readText = File.ReadAllLines(path);
        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