File.ReadAllText 方法

定義

開啟文字檔,將檔案中的所有文字讀入字串,然後關閉該檔案。

多載

ReadAllText(String)

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

ReadAllText(String, Encoding)

開啟檔案,利用指定的編碼方式讀取檔案中的所有文字,然後關閉該檔案。

ReadAllText(String)

來源:
File.cs
來源:
File.cs
來源:
File.cs

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

public:
 static System::String ^ ReadAllText(System::String ^ path);
public static string ReadAllText (string path);
static member ReadAllText : string -> string
Public Shared Function ReadAllText (path As String) As String

參數

path
String

要開啟用於讀取的檔案。

傳回

包含檔案中所有文字的字串。

例外狀況

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

pathnull

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

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

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

path 指定了唯讀的檔案。

-或-

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

-或-

path 指定了目錄。

-或-

呼叫端沒有必要的權限。

找不到 path 指定的檔案。

path 格式無效。

呼叫端沒有必要的權限。

範例

下列程式代碼範例示範如何使用 ReadAllText 方法來顯示檔案的內容。 在此範例中,如果檔案不存在,則會建立檔案,並將文字新增至該檔案。

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" + Environment.NewLine;
            File.WriteAllText(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.ReadAllText(path);
        Console.WriteLine(readText);
    }
}
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" + Environment.NewLine

    File.WriteAllText(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.ReadAllText path
printfn $"{readText}"
Imports System.IO
Imports System.Text

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

        ' 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" + Environment.NewLine
            File.WriteAllText(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.ReadAllText(path)
        Console.WriteLine(readText)
    End Sub
End Class

備註

這個方法會開啟檔案、讀取檔案中的所有文字,並以字串的形式傳回它。 然後,它會關閉檔案。

這個方法會根據位元組順序標記的存在,嘗試自動偵測檔案的編碼方式。 它會自動辨識 UTF-8、小端 UTF-16、big-endian UTF-16、small-endian UTF-32 和 big-endian UTF-32 文字,如果檔案以適當的位元組順序標記開頭。

ReadAllText(String, Encoding)讀取可能包含匯入文字的檔案時,請使用 方法多載,因為無法辨識的字元可能無法正確讀取。

即使引發例外狀況,這個方法仍保證會關閉檔案句柄。

另請參閱

適用於

ReadAllText(String, Encoding)

來源:
File.cs
來源:
File.cs
來源:
File.cs

開啟檔案,利用指定的編碼方式讀取檔案中的所有文字,然後關閉該檔案。

public:
 static System::String ^ ReadAllText(System::String ^ path, System::Text::Encoding ^ encoding);
public static string ReadAllText (string path, System.Text.Encoding encoding);
static member ReadAllText : string * System.Text.Encoding -> string
Public Shared Function ReadAllText (path As String, encoding As Encoding) As String

參數

path
String

要開啟用於讀取的檔案。

encoding
Encoding

套用至檔案內容的編碼方式。

傳回

包含檔案中所有文字的字串。

例外狀況

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

pathnull

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

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

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

path 指定了唯讀的檔案。

-或-

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

-或-

path 指定了目錄。

-或-

呼叫端沒有必要的權限。

找不到 path 指定的檔案。

path 格式無效。

呼叫端沒有必要的權限。

範例

下列程式代碼範例示範如何使用 ReadAllText 方法來顯示檔案的內容。 在此範例中,如果檔案不存在,則會建立檔案,並將文字新增至該檔案。

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" + Environment.NewLine;
            File.WriteAllText(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.ReadAllText(path);
        Console.WriteLine(readText);
    }
}
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" + Environment.NewLine

    File.WriteAllText(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.ReadAllText path
printfn $"{readText}"
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" + Environment.NewLine
            File.WriteAllText(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.ReadAllText(path)
        Console.WriteLine(readText)
    End Sub
End Class

備註

這個方法會開啟檔案、讀取檔案中的所有文字,並以字串的形式傳回它。 然後,它會關閉檔案。

這個方法會根據位元組順序標記的存在,嘗試自動偵測檔案的編碼方式。 它會自動辨識 UTF-8、小端 UTF-16、big-endian UTF-16、small-endian UTF-32 和 big-endian UTF-32 文字,如果檔案以適當的位元組順序標記開頭。

即使引發例外狀況,這個方法仍保證會關閉檔案句柄。

若要使用針對作業系統所設定的編碼設定,請指定 Encoding.Default 參數的 encoding 屬性。

另請參閱

適用於