File.ReadAllText File.ReadAllText File.ReadAllText File.ReadAllText Method

定義

開啟文字檔,將檔案中的所有文字讀入字串,然後關閉該檔案。Opens a text file, reads all the text in the file into a string, and then closes the file.

多載

ReadAllText(String, Encoding) ReadAllText(String, Encoding) ReadAllText(String, Encoding)

開啟檔案,利用指定的編碼方式讀取檔案中的所有文字,然後關閉該檔案。Opens a file, reads all text in the file with the specified encoding, and then closes the file.

ReadAllText(String) ReadAllText(String) ReadAllText(String) ReadAllText(String)

開啟文字檔,讀取檔案中的所有文字,然後關閉該檔案。Opens a text file, reads all the text in the file, and then closes the file.

ReadAllText(String, Encoding) ReadAllText(String, Encoding) ReadAllText(String, Encoding)

開啟檔案,利用指定的編碼方式讀取檔案中的所有文字,然後關閉該檔案。Opens a file, reads all text in the file with the specified encoding, and then closes the file.

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

參數

path
String String String String

要開啟用於讀取的檔案。The file to open for reading.

encoding
Encoding Encoding Encoding Encoding

套用至檔案內容的編碼方式。The encoding applied to the contents of the file.

傳回

包含檔案中所有文字的字串。A string containing all text in the file.

例外狀況

path 為零長度字串,只包含空格,或包含一或多個如 InvalidPathChars 所定義的無效字元。path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。The specified path, file name, or both exceed the system-defined maximum length.

指定的路徑無效 (例如,它位於未對應的磁碟機上)。The specified path is invalid (for example, it is on an unmapped drive).

開啟檔案時發生 I/O 錯誤。An I/O error occurred while opening the file.

path 指定了唯讀的檔案。path specified a file that is read-only.

-或--or- 這個作業在目前平台不受支援。This operation is not supported on the current platform.

-或--or- path 指定了目錄。path specified a directory.

-或--or- 呼叫端沒有必要的使用權限。The caller does not have the required permission.

找不到 path 指定的檔案。The file specified in path was not found.

呼叫端沒有必要的使用權限。The caller does not have the required permission.

範例

下列程式碼範例示範使用ReadAllText方法,以顯示檔案的內容。The following code example demonstrates the use of the ReadAllText method to display the contents of a file. 在此範例會建立檔案,如果它尚未存在,而且文字已加入至它所示。In this example a file is created, if it doesn't already exist, and text is added to it.

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

備註

這個方法會開啟一個檔案,讀取在檔案中的所有文字,並以字串形式傳回它。This method opens a file, reads all the text in the file, and returns it as a string. 然後,它會關閉檔案。It then closes the file.

這個方法會嘗試自動偵測編碼的位元組順序標記存在為基礎的檔案。This method attempts to automatically detect the encoding of a file based on the presence of byte order marks. 編碼格式的 utf-8 和 UTF-32 (big endian 和位元組由小到大) 可以偵測到。Encoding formats UTF-8 and UTF-32 (both big-endian and little-endian) can be detected.

檔案控制代碼保證會關閉這個方法,即使會引發例外狀況。The file handle is guaranteed to be closed by this method, even if exceptions are raised.

若要設定適用於您作業系統,請使用編碼方式的設定,指定Encoding.Default屬性encoding參數。To use the encoding settings as configured for your operating system, specify the Encoding.Default property for the encoding parameter.

安全性

FileIOPermission
針對從檔案或目錄的讀取權限。for access to read from a file or directory. 相關聯的列舉型別: ReadAssociated enumeration: Read

另請參閱

ReadAllText(String) ReadAllText(String) ReadAllText(String) ReadAllText(String)

開啟文字檔,讀取檔案中的所有文字,然後關閉該檔案。Opens a text file, reads all the text in the file, and then closes the file.

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 String String String

要開啟用於讀取的檔案。The file to open for reading.

傳回

包含檔案中所有文字的字串。A string containing all the text in the file.

例外狀況

path 為零長度字串,只包含空格,或包含一或多個如 InvalidPathChars 所定義的無效字元。path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。The specified path, file name, or both exceed the system-defined maximum length.

指定的路徑無效 (例如,它位於未對應的磁碟機上)。The specified path is invalid (for example, it is on an unmapped drive).

開啟檔案時發生 I/O 錯誤。An I/O error occurred while opening the file.

path 指定了唯讀的檔案。path specified a file that is read-only.

-或--or- 這個作業在目前平台不受支援。This operation is not supported on the current platform.

-或--or- path 指定了目錄。path specified a directory.

-或--or- 呼叫端沒有必要的使用權限。The caller does not have the required permission.

找不到 path 指定的檔案。The file specified in path was not found.

呼叫端沒有必要的使用權限。The caller does not have the required permission.

範例

下列程式碼範例示範使用ReadAllText方法,以顯示檔案的內容。The following code example demonstrates the use of the ReadAllText method to display the contents of a file. 在此範例會建立檔案,如果它尚未存在,而且文字已加入至它所示。In this example a file is created, if it doesn't already exist, and text is added to it.

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

備註

這個方法會開啟一個檔案,讀取在檔案中的所有文字,並以字串形式傳回它。This method opens a file, reads all the text in the file, and returns it as a string. 然後,它會關閉檔案。It then closes the file.

這個方法會嘗試自動偵測編碼的位元組順序標記存在為基礎的檔案。This method attempts to automatically detect the encoding of a file based on the presence of byte order marks. 編碼格式的 utf-8 和 UTF-32 (big endian 和位元組由小到大) 可以偵測到。Encoding formats UTF-8 and UTF-32 (both big-endian and little-endian) can be detected.

使用ReadAllText(String, Encoding)方法多載時讀取檔案,其中可能包含匯入的文字,因為無法辨識的字元可能無法正確讀取。Use the ReadAllText(String, Encoding) method overload when reading files that might contain imported text, because unrecognized characters may not be read correctly.

檔案控制代碼保證會關閉這個方法,即使會引發例外狀況。The file handle is guaranteed to be closed by this method, even if exceptions are raised.

安全性

FileIOPermission
針對從檔案或目錄的讀取權限。for access to read from a file or directory. 相關聯的列舉型別: ReadAssociated enumeration: Read

另請參閱

適用於