Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
ReadLines Method (String, Encoding)
Collapse the table of content
Expand the table of content

File.ReadLines Method (String, Encoding)

Read the lines of a file that has a specified encoding.

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

static member ReadLines : 
        path:string * 
        encoding:Encoding -> IEnumerable<string> 


Type: System.String

The file to read.

Type: System.Text.Encoding

The encoding that is applied to the contents of the file.

Return Value

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


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


path is a null reference (Nothing in Visual Basic).


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


The file specified by path was not found.


An I/O error occurred while opening the file.


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.


The caller does not have the required permission.


path specifies a file that is read-only.


This operation is not supported on the current platform.


path is a directory.


The caller does not have the required permission.

Use this method to specify an encoding to use read the file.

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:

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4
© 2015 Microsoft