File.ReadLines 方法

定义

读取文件的行。

重载

ReadLines(String, Encoding)

读取具有指定编码的文件的行。

ReadLines(String)

读取文件的行。

ReadLines(String, Encoding)

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

读取具有指定编码的文件的行。

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

参数

path
String

要读取的文件。

encoding
Encoding

应用于文件内容的编码。

返回

文件的所有行或查询结果的行。

例外

低于 2.1 的 .NET Framework 和 .NET Core 版本:path 为零长度的字符串,仅包含空格,或包含由 GetInvalidPathChars() 方法定义的一个或多个无效字符。

path null

path 无效(例如,它位于未映射的驱动器上)。

找不到由 path 指定的文件。

打开文件时出现 I/O 错误。

path 超过系统定义的最大长度。

调用方没有所需的权限。

path 指定只读文件。

-或-

当前平台上不支持此操作。

-或-

path 是目录。

-或-

调用方没有所需的权限。

注解

使用此方法指定用于读取文件的编码。

ReadLinesReadAllLines 方法不同,如下所示:使用 ReadLines时,可以在返回整个集合之前开始枚举字符串的集合。 使用 ReadAllLines时,必须等待返回整个字符串数组,然后才能访问该数组。 因此,在使用非常大的文件时,ReadLines 效率更高。

可以使用 ReadLines 方法执行以下操作:

适用于

.NET 9 和其他版本
产品 版本
.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

读取文件的行。

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

参数

path
String

要读取的文件。

返回

文件的所有行或查询结果的行。

例外

低于 2.1 的 .NET Framework 和 .NET Core 版本:path 是一个零长度的字符串,仅包含空格,或者包含由 GetInvalidPathChars() 方法定义的一个或多个无效字符。

path null

path 无效(例如,它位于未映射的驱动器上)。

找不到由 path 指定的文件。

打开文件时出现 I/O 错误。

path 超过系统定义的最大长度。

调用方没有所需的权限。

path 指定只读文件。

-或-

当前平台上不支持此操作。

-或-

path 是目录。

-或-

调用方没有所需的权限。

示例

以下示例读取文件的行,以查找包含指定字符串的行。

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

以下示例在 LINQ 查询中使用 ReadLines 方法,该查询枚举具有 .txt 扩展名的文件的所有目录,读取文件的每一行,并在包含字符串“Microsoft”时显示该行。

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);
        }
    }
}

注解

ReadLinesReadAllLines 方法不同,如下所示:使用 ReadLines时,可以在返回整个集合之前开始枚举字符串的集合;使用 ReadAllLines时,必须先等待返回整个字符串数组,然后才能访问数组。 因此,在使用非常大的文件时,ReadLines 效率更高。

可以使用 ReadLines 方法执行以下操作:

此方法对编码值使用 UTF8

适用于

.NET 9 和其他版本
产品 版本
.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