File.ReadLines File.ReadLines File.ReadLines File.ReadLines Method

Definition

Reads the lines of a file.

Overloads

ReadLines(String) ReadLines(String) ReadLines(String) ReadLines(String)

Reads the lines of a file.

ReadLines(String, Encoding) ReadLines(String, Encoding) ReadLines(String, Encoding)

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

ReadLines(String) ReadLines(String) ReadLines(String) ReadLines(String)

Reads the lines of a file.

public:
 static System::Collections::Generic::IEnumerable<System::String ^> ^ ReadLines(System::String ^ path);
public static System.Collections.Generic.IEnumerable<string> ReadLines (string path);
static member ReadLines : string -> seq<string>
Public Shared Function ReadLines (path As String) As IEnumerable(Of String)
Parameters
path
String String String String

The file to read.

Returns

All the lines of the file, or the lines that are the result of a query.

Exceptions

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

An I/O error occurred while opening the file.

The caller does not have the required permission.

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.

Examples

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);
    }
}
For Each line As String In File.ReadLines("d:\data\episodes.txt")
    If line.Contains("episode") And line.Contains("2006") Then
        Console.WriteLine(line)
    End If
Next 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);
        }
    }
}
Imports System.IO
Imports System.Xml.Linq
Module Module1

    Sub Main()
        Try
            Dim files = From chkFile In Directory.EnumerateFiles("c:\", "*.txt", SearchOption.AllDirectories)
                        From line In File.ReadLines(chkFile)
                        Where line.Contains("Microsoft")
                        Select New With {.curFile = chkFile, .curLine = line}

            For Each f In files
                Console.WriteLine("{0}\t{1}", f.curFile, f.curLine)
            Next
            Console.WriteLine("{0} files found.", files.Count.ToString())
        Catch UAEx As UnauthorizedAccessException
            Console.WriteLine(UAEx.Message)
        Catch PathEx As PathTooLongException
            Console.WriteLine(PathEx.Message)
        End Try
    End Sub
End Module

Remarks

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.

ReadLines(String, Encoding) ReadLines(String, Encoding) ReadLines(String, Encoding)

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

public:
 static System::Collections::Generic::IEnumerable<System::String ^> ^ ReadLines(System::String ^ path, System::Text::Encoding ^ encoding);
public static System.Collections.Generic.IEnumerable<string> ReadLines (string path, System.Text.Encoding encoding);
static member ReadLines : string * System.Text.Encoding -> seq<string>
Parameters
path
String String String String

The file to read.

encoding
Encoding Encoding Encoding Encoding

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

Returns

All the lines of the file, or the lines that are the result of a query.

Exceptions

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

An I/O error occurred while opening the file.

The caller does not have the required permission.

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.

Remarks

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:

Applies to