Lezen in het Engels Bewerken

Share via


File.ReadLines Method

Definition

Reads the lines of a file.

Overloads

ReadLines(String, Encoding)

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

ReadLines(String)

Reads the lines of a file.

ReadLines(String, Encoding)

Source:
File.cs
Source:
File.cs
Source:
File.cs

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

C#
public static System.Collections.Generic.IEnumerable<string> ReadLines (string path, System.Text.Encoding encoding);

Parameters

path
String

The file to read.

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

.NET Framework and .NET Core versions older than 2.1: 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 null.

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.

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

.NET 9 en andere versies
Product Versies
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ReadLines(String)

Source:
File.cs
Source:
File.cs
Source:
File.cs

Reads the lines of a file.

C#
public static System.Collections.Generic.IEnumerable<string> ReadLines (string path);

Parameters

path
String

The file to read.

Returns

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

Exceptions

.NET Framework and .NET Core versions older than 2.1: path is a zero-length string, contains only white space, or contains one or more invalid characters defined by the GetInvalidPathChars() method.

path is null.

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.

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.

C#
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".

C#
using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            // Set a variable to the My Documents path.
            string docPath =
            Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            var files = from file in Directory.EnumerateFiles(docPath, "*.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($"{f.File}\t{f.Line}");
            }
            Console.WriteLine($"{files.Count().ToString()} files found.");
        }
        catch (UnauthorizedAccessException uAEx)
        {
            Console.WriteLine(uAEx.Message);
        }
        catch (PathTooLongException pathEx)
        {
            Console.WriteLine(pathEx.Message);
        }
    }
}

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.

Applies to

.NET 9 en andere versies
Product Versies
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0