File.ReadAllLines 方法

定義

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

多載

ReadAllLines(String)

開啟文字檔,讀取檔案的所有行,然後關閉檔案。

ReadAllLines(String, Encoding)

開啟檔案,以指定的編碼方式讀取檔案的所有行,然後關閉檔案。

ReadAllLines(String)

來源:
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 和 2.1 之前的 .NET Core 版本:path是長度為零的字串、只包含空格符,或包含一或多個無效的字元。 您可以使用 GetInvalidPathChars() 方法查詢無效字元。

pathnull

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

指定的路徑無效 (例如,它位於未對應的磁碟機上)。

開啟檔案時發生 I/O 錯誤。

path 指定了唯讀的檔案。

-或-

這個作業在目前平台不受支援。

-或-

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') ,或緊接換行字元的歸位字元。 產生的字串不包含終止歸位字元和/或換行字元。

這個方法會根據位元組順序標記的存在,嘗試自動偵測檔案的編碼方式。 可以偵測到編碼格式 UTF-8 和 UTF-32 (big-endian 和 small-endian) 。

另請參閱

適用於

ReadAllLines(String, Encoding)

來源:
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 和 2.1 之前的 .NET Core 版本:path是長度為零的字串、只包含空格符,或包含一或多個無效的字元。 您可以使用 GetInvalidPathChars() 方法查詢無效字元。

pathnull

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

指定的路徑無效 (例如,它位於未對應的磁碟機上)。

開啟檔案時發生 I/O 錯誤。

path 指定了唯讀的檔案。

-或-

這個作業在目前平台不受支援。

-或-

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') ,或緊接換行字元的歸位字元。 產生的字串不包含終止歸位字元和/或換行字元。

這個方法會根據位元組順序標記的存在,嘗試自動偵測檔案的編碼方式。 可以偵測到編碼格式 UTF-8 和 UTF-32 (big-endian 和 small-endian) 。

另請參閱

適用於