File.ReadAllLines 方法

定义

打开文本文件,将文件的所有行读入字符串数组,然后关闭该文件。

重载

名称 说明
ReadAllLines(String)

打开文本文件,读取文件的所有行,然后关闭该文件。

ReadAllLines(String, Encoding)

打开文件,读取具有指定编码的文件的所有行,然后关闭该文件。

ReadAllLines(String)

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

打开文本文件,读取文件的所有行,然后关闭该文件。

public:
 static cli::array <System::String ^> ^ ReadAllLines(System::String ^ path);
public static string[] ReadAllLines(string path);
static member ReadAllLines : string -> string[]
Public Shared Function ReadAllLines (path As String) As String()

参数

path
String

要打开以供读取的文件。

返回

String[]

包含文件的所有行的字符串数组。

例外

低于 2.1 的 .NET Framework 和 .NET Core 版本: path 是长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用该方法 GetInvalidPathChars() 查询无效字符。

pathnull

指定的路径、文件名或两者都超过了系统定义的最大长度。

指定的路径无效(例如,它位于未映射的驱动器上)。

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

当前平台上不支持此作。

-或-

path 指定了目录。

-或-

调用方没有所需的权限。

找不到在其中 path 指定的文件。

path 格式无效。

调用方没有所需的权限。

示例

下面的代码示例演示如何使用 ReadAllLines 该方法显示文件的内容。 在此示例中,创建了一个文件(如果该文件尚不存在),并将文本添加到其中。

using System;
using System.IO;
class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file.
        if (!File.Exists(path))
        {
            // Create a file to write to.
            string[] createText = { "Hello", "And", "Welcome" };
            File.WriteAllLines(path, createText);
        }

        // This text is always added, making the file longer over time
        // if it is not deleted.
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText);

        // Open the file to read from.
        string[] readText = File.ReadAllLines(path);
        foreach (string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}
open System
open System.IO

let path = @"c:\temp\MyTest.txt"

// This text is added only once to the file.
if File.Exists path |> not then
    // Create a file to write to.
    let createText = [ "Hello"; "And"; "Welcome" ]
    File.WriteAllLines(path, createText)

// This text is always added, making the file longer over time
// if it is not deleted.
let appendText =
    "This is extra text" + Environment.NewLine

File.AppendAllText(path, appendText)

// Open the file to read from.
let readText = File.ReadAllLines path

for s in readText do
    printfn $"{s}"
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        Dim sw As StreamWriter

        ' This text is added only once to the file.
        If File.Exists(path) = False Then

            ' Create a file to write to.
            Dim createText() As String = {"Hello", "And", "Welcome"}
            File.WriteAllLines(path, createText)
        End If

        ' This text is always added, making the file longer over time
        ' if it is not deleted.
        Dim appendText As String = "This is extra text" + Environment.NewLine
        File.AppendAllText(path, appendText)

        ' Open the file to read from.
        Dim readText() As String = File.ReadAllLines(path)
        Dim s As String
        For Each s In readText
            Console.WriteLine(s)
        Next
    End Sub
End Class

注解

此方法打开一个文件,读取文件的每一行,然后将每行添加为字符串数组的元素。 然后关闭该文件。 行定义为后跟回车符(“\r”)、换行符(“\n”)或紧跟换行符的回车符序列。 生成的字符串不包含终止回车符和/或换行符。

如果文件以换行序列结尾,则不会向数组追加其他空行。 例如,包含"line1\nline2\n"的文件生成与包含"line1\nline2"的文件相同的双元素数组 (["line1", "line2"]) 。

此方法尝试根据字节顺序标记的存在自动检测文件的编码。 可以检测 UTF-8 和 UTF-32(big-endian 和 little-endian)的编码格式。

另请参阅

适用于

ReadAllLines(String, Encoding)

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

打开文件,读取具有指定编码的文件的所有行,然后关闭该文件。

public:
 static cli::array <System::String ^> ^ ReadAllLines(System::String ^ path, System::Text::Encoding ^ encoding);
public static string[] ReadAllLines(string path, System.Text.Encoding encoding);
static member ReadAllLines : string * System.Text.Encoding -> string[]
Public Shared Function ReadAllLines (path As String, encoding As Encoding) As String()

参数

path
String

要打开以供读取的文件。

encoding
Encoding

应用于文件内容的编码。

返回

String[]

包含文件的所有行的字符串数组。

例外

低于 2.1 的 .NET Framework 和 .NET Core 版本: path 是长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用该方法 GetInvalidPathChars() 查询无效字符。

pathnull

指定的路径、文件名或两者都超过了系统定义的最大长度。

指定的路径无效(例如,它位于未映射的驱动器上)。

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

当前平台上不支持此作。

-或-

path 指定了目录。

-或-

调用方没有所需的权限。

找不到在其中 path 指定的文件。

path 格式无效。

调用方没有所需的权限。

示例

下面的代码示例演示如何使用 ReadAllLines 该方法显示文件的内容。 在此示例中,创建了一个文件(如果该文件尚不存在),并将文本添加到其中。

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file.
        if (!File.Exists(path))
        {
            // Create a file to write to.
            string[] createText = { "Hello", "And", "Welcome" };
            File.WriteAllLines(path, createText, Encoding.UTF8);
        }

        // This text is always added, making the file longer over time
        // if it is not deleted.
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText, Encoding.UTF8);

        // Open the file to read from.
        string[] readText = File.ReadAllLines(path, Encoding.UTF8);
        foreach (string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}
open System
open System.IO
open System.Text

let path = @"c:\temp\MyTest.txt"

// This text is added only once to the file.
if File.Exists path |> not then
    // Create a file to write to.
    let createText = [ "Hello"; "And"; "Welcome" ]
    File.WriteAllLines(path, createText, Encoding.UTF8)

// This text is always added, making the file longer over time
// if it is not deleted.
let appendText =
    "This is extra text" + Environment.NewLine

File.AppendAllText(path, appendText, Encoding.UTF8)

// Open the file to read from.
let readText = File.ReadAllLines(path, Encoding.UTF8)

for s in readText do
    printfn $"{s}"
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        Dim sw As StreamWriter

        ' This text is added only once to the file.
        If File.Exists(path) = False Then

            ' Create a file to write to.
            Dim createText() As String = {"Hello", "And", "Welcome"}
            File.WriteAllLines(path, createText, Encoding.UTF8)
        End If

        ' This text is always added, making the file longer over time
        ' if it is not deleted.
        Dim appendText As String = "This is extra text" + Environment.NewLine
        File.AppendAllText(path, appendText, Encoding.UTF8)

        ' Open the file to read from.
        Dim readText() As String = File.ReadAllLines(path, Encoding.UTF8)
        Dim s As String
        For Each s In readText
            Console.WriteLine(s)
        Next
    End Sub
End Class

注解

此方法打开一个文件,读取文件的每一行,然后将每行添加为字符串数组的元素。 然后关闭该文件。 行定义为后跟回车符(“\r”)、换行符(“\n”)或紧跟换行符的回车符序列。 生成的字符串不包含终止回车符和/或换行符。

如果文件以换行序列结尾,则不会向数组追加其他空行。 例如,包含"line1\nline2\n"的文件生成与包含"line1\nline2"的文件相同的双元素数组 (["line1", "line2"]) 。

此方法尝试根据字节顺序标记的存在自动检测文件的编码。 可以检测 UTF-8 和 UTF-32(big-endian 和 little-endian)的编码格式。

另请参阅

适用于