File.ReadAllLines 方法

定義

打開一個文字檔,將檔案的所有行讀入字串陣列,然後關閉該檔案。

多載

名稱 Description
ReadAllLines(String)

打開一個文字檔,讀取檔案的所有行數,然後關閉該檔案。

ReadAllLines(String, Encoding)

開啟一個檔案,讀取指定編碼的所有行,然後關閉該檔案。

ReadAllLines(String)

來源:
File.cs
來源:
File.cs
來源:
File.cs
來源:
File.cs
來源:
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[]

一個包含檔案所有行的字串陣列。

例外狀況

.NET Framework 與 .NET Core 版本 2.1 以前: 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", "line2"]) 與包含 "line1\nline2"的檔案相同。

此方法嘗試根據位元組順序標記的存在自動偵測檔案的編碼。 可偵測 UTF-8 與 UTF-32(大端與小端)編碼格式。

另請參閱

適用於

ReadAllLines(String, Encoding)

來源:
File.cs
來源:
File.cs
來源:
File.cs
來源:
File.cs
來源:
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[]

一個包含檔案所有行的字串陣列。

例外狀況

.NET Framework 與 .NET Core 版本 2.1 以前: 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", "line2"]) 與包含 "line1\nline2"的檔案相同。

此方法嘗試根據位元組順序標記的存在自動偵測檔案的編碼。 可偵測 UTF-8 與 UTF-32(大端與小端)編碼格式。

另請參閱

適用於