File.AppendAllText 方法

定義

將指定字串附加至檔案,如果檔案不存在,請建立一個新檔案。

多載

AppendAllText(String, String)

開啟檔案,將指定的字串附加至檔案,然後關閉檔案。 如果檔案不存在,則這個方法會建立檔案,將指定的字串寫入檔案,然後關閉檔案。

AppendAllText(String, String, Encoding)

使用指定編碼方式將指定字串附加至檔案,如果檔案不存在,請建立一個新檔案。

AppendAllText(String, String)

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

開啟檔案,將指定的字串附加至檔案,然後關閉檔案。 如果檔案不存在,則這個方法會建立檔案,將指定的字串寫入檔案,然後關閉檔案。

public:
 static void AppendAllText(System::String ^ path, System::String ^ contents);
public static void AppendAllText (string path, 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

要將指定字串附加至其中的檔案。

contents
String

要附加至檔案的字串。

例外狀況

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

pathnull

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

指定的路徑無效 (例如,此目錄不存在或位於未對應的磁碟機上)。

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

path 指定了唯讀的檔案。

-或-

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

-或-

path 指定了目錄。

-或-

呼叫端沒有必要的權限。

path 格式無效。

呼叫端沒有必要的權限。

範例

下列程式碼範例示範如何使用 AppendAllText 方法,將額外的文字新增至檔案結尾。 在此範例中,如果檔案不存在,則會建立檔案,並將文字新增至該檔案。 不過,在磁片磁碟機 C 上命名 temp 的目錄必須存在,範例才能順利完成。

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

備註

指定字串和檔案路徑時,這個方法會開啟指定的檔案、將字串附加至檔案結尾,然後關閉檔案。 即使引發例外狀況,這個方法仍保證會關閉檔案控制代碼。

如果檔案不存在,則方法會建立檔案,但不會建立新的目錄。 因此,參數的值 path 必須包含現有的目錄。

適用於

AppendAllText(String, String, Encoding)

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

使用指定編碼方式將指定字串附加至檔案,如果檔案不存在,請建立一個新檔案。

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);
public static void AppendAllText (string path, string? contents, System.Text.Encoding encoding);
static member AppendAllText : string * string * System.Text.Encoding -> unit
Public Shared Sub AppendAllText (path As String, contents As String, encoding As Encoding)

參數

path
String

要將指定字串附加至其中的檔案。

contents
String

要附加至檔案的字串。

encoding
Encoding

要使用的字元編碼。

例外狀況

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

pathnull

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

指定的路徑無效 (例如,此目錄不存在或位於未對應的磁碟機上)。

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

path 指定了唯讀的檔案。

-或-

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

-或-

path 指定了目錄。

-或-

呼叫端沒有必要的權限。

path 格式無效。

呼叫端沒有必要的權限。

範例

下列程式碼範例示範如何使用 AppendAllText 方法,將額外的文字新增至檔案結尾。 在此範例中,如果檔案不存在,則會建立檔案,並將文字新增至該檔案。 不過,在磁片磁碟機 C 上命名 temp 的目錄必須存在,範例才能順利完成。

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

備註

指定字串和檔案路徑時,這個方法會開啟指定的檔案、使用指定的編碼將字串附加至檔案的結尾,然後關閉檔案。 即使引發例外狀況,這個方法仍保證會關閉檔案控制代碼。

如果檔案不存在,則方法會建立檔案,但不會建立新的目錄。 因此,參數的值 path 必須包含現有的目錄。

適用於