File.AppendAllText File.AppendAllText File.AppendAllText File.AppendAllText Method

定義

將指定字串附加至檔案,如果檔案不存在,請建立一個新檔案。Appends the specified string to the file, creating the file if it does not already exist.

多載

AppendAllText(String, String) AppendAllText(String, String) AppendAllText(String, String) AppendAllText(String, String)

開啟檔案,將指定的字串附加至檔案,然後關閉檔案。Opens a file, appends the specified string to the file, and then closes the file. 如果檔案不存在,則這個方法會建立檔案,將指定的字串寫入檔案,然後關閉檔案。If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.

AppendAllText(String, String, Encoding) AppendAllText(String, String, Encoding) AppendAllText(String, String, Encoding)

使用指定編碼方式將指定字串附加至檔案,如果檔案不存在,請建立一個新檔案。Appends the specified string to the file using the specified encoding, creating the file if it does not already exist.

AppendAllText(String, String) AppendAllText(String, String) AppendAllText(String, String) AppendAllText(String, String)

開啟檔案,將指定的字串附加至檔案,然後關閉檔案。Opens a file, appends the specified string to the file, and then closes the file. 如果檔案不存在,則這個方法會建立檔案,將指定的字串寫入檔案,然後關閉檔案。If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.

public:
 static void AppendAllText(System::String ^ path, System::String ^ contents);
public static void AppendAllText (string path, string contents);
static member AppendAllText : string * string -> unit
Public Shared Sub AppendAllText (path As String, contents As String)

參數

path
String String String String

要將指定字串附加至其中的檔案。The file to append the specified string to.

contents
String String String String

要附加至檔案的字串。The string to append to 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, the directory doesn't exist or 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.

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

範例

下列程式碼範例示範如何使用AppendAllText方法,在檔案結尾加上額外的文字。The following code example demonstrates the use of the AppendAllText method to add extra text to the end of a file. 在此範例中,如果檔案不存在,就會建立檔案,並將文字加入其中。In this example, a file is created if it doesn't already exist, and text is added to it. 不過,在 C 磁片temp磁碟機上命名的目錄必須存在,範例才能順利完成。However, the directory named temp on drive C must exist for the example to complete successfully.

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

備註

指定字串和檔案路徑時,這個方法會開啟指定的檔案,並將字串附加至檔案結尾,然後關閉檔案。Given a string and a file path, this method opens the specified file, appends the string to the end of the file, and then closes the file. 即使引發例外狀況,也一定會由這個方法關閉檔案控制代碼。The file handle is guaranteed to be closed by this method, even if exceptions are raised.

方法會建立檔案(如果不存在),但不會建立新的目錄。The method creates the file if it doesn't exist, but it doesn't create new directories. 因此,此path參數的值必須包含現有的目錄。Therefore, the value of the path parameter must contain existing directories.

安全性

FileIOPermission
適用于寫入檔案或目錄的存取權。for access to write to a file or directory. 相關聯的列舉:AppendAssociated enumeration: Append

AppendAllText(String, String, Encoding) AppendAllText(String, String, Encoding) AppendAllText(String, String, Encoding)

使用指定編碼方式將指定字串附加至檔案,如果檔案不存在,請建立一個新檔案。Appends the specified string to the file using the specified encoding, creating the file if it does not already exist.

public:
 static void AppendAllText(System::String ^ path, System::String ^ contents, System::Text::Encoding ^ encoding);
public static void AppendAllText (string path, string contents, System.Text.Encoding encoding);
static member AppendAllText : string * string * System.Text.Encoding -> unit

參數

path
String String String String

要將指定字串附加至其中的檔案。The file to append the specified string to.

contents
String String String String

要附加至檔案的字串。The string to append to the file.

encoding
Encoding Encoding Encoding Encoding

要使用的字元編碼。The character encoding to use.

例外狀況

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, the directory doesn't exist or 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.

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

範例

下列程式碼範例示範如何使用AppendAllText方法,在檔案結尾加上額外的文字。The following code example demonstrates the use of the AppendAllText method to add extra text to the end of a file. 在此範例中,如果檔案不存在,就會建立檔案,並將文字加入其中。In this example, a file is created if it doesn't already exist, and text is added to it. 不過,在 C 磁片temp磁碟機上命名的目錄必須存在,範例才能順利完成。However, the directory named temp on drive C must exist for the example to complete successfully.

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

備註

指定字串和檔案路徑時,這個方法會開啟指定的檔案,並使用指定的編碼方式將字串附加至檔案結尾,然後關閉檔案。Given a string and a file path, this method opens the specified file, appends the string to the end of the file using the specified encoding, and then closes the file. 即使引發例外狀況,也一定會由這個方法關閉檔案控制代碼。The file handle is guaranteed to be closed by this method, even if exceptions are raised.

方法會建立檔案(如果不存在),但不會建立新的目錄。The method creates the file if it doesn't exist, but it doesn't create new directories. 因此,此path參數的值必須包含現有的目錄。Therefore, the value of the path parameter must contain existing directories.

安全性

FileIOPermission
適用于寫入檔案或目錄的存取權。for access to write to a file or directory. 相關聯的列舉:AppendAssociated enumeration: Append

適用於