File.ReadLines Method (String)

Reads the lines of a file.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public static IEnumerable<string> ReadLines(
	string path
)

Parameters

path
Type: System.String

The file to read.

Return Value

Type: System.Collections.Generic.IEnumerable<String>
All the lines of the file, or the lines that are the result of a query.

ExceptionCondition
ArgumentException

path is a zero-length string, contains only white space, or contains one or more invalid characters defined by the GetInvalidPathChars method.

ArgumentNullException

path is null.

DirectoryNotFoundException

path is invalid (for example, it is on an unmapped drive).

FileNotFoundException

The file specified by path was not found.

IOException

An I/O error occurred while opening the file.

PathTooLongException

path exceeds the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.

SecurityException

The caller does not have the required permission.

UnauthorizedAccessException

path specifies a file that is read-only.

-or-

This operation is not supported on the current platform.

-or-

path is a directory.

-or-

The caller does not have the required permission.

The ReadLines and ReadAllLines methods differ as follows: When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned; when you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines can be more efficient.

You can use the ReadLines method to do the following:

This method uses UTF8 for the encoding value.

The following example reads the lines of a file to find lines that contain specified strings.

foreach (string line in File.ReadLines(@"d:\data\episodes.txt"))
{
    if (line.Contains("episode") & line.Contains("2006"))
    {
        Console.WriteLine(line);
    }
}

The following example uses the ReadLines method in a LINQ query that enumerates all directories for files that have a .txt extension, reads each line of the file, and displays the line if it contains the string "Microsoft".

using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            var files = from file in Directory.EnumerateFiles(@"c:\", "*.txt", SearchOption.AllDirectories)
                        from line in File.ReadLines(file)
                        where line.Contains("Microsoft")
                        select new
                        {
                            File = file,
                            Line = line
                        };

            foreach (var f in files)
            {
                Console.WriteLine("{0}\t{1}", f.File, f.Line);
            }
			Console.WriteLine("{0} files found.", files.Count().ToString());
        }
        catch (UnauthorizedAccessException UAEx)
        {
            Console.WriteLine(UAEx.Message);
        }
        catch (PathTooLongException PathEx)
        {
            Console.WriteLine(PathEx.Message);
        }
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft