共用方式為


File.AppendAllText 方法

定義

將指定的字串附加至檔案,如果檔案不存在,則會建立檔案。

多載

AppendAllText(String, ReadOnlySpan<Char>)

將指定的字串附加至檔案,如果檔案不存在,則會建立檔案。

AppendAllText(String, String)

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

AppendAllText(String, ReadOnlySpan<Char>, Encoding)

將指定的字串附加至檔案,如果檔案不存在,則會建立檔案。

AppendAllText(String, String, Encoding)

使用指定的編碼將指定的字串附加至檔案,如果檔案不存在,則建立檔案。

AppendAllText(String, ReadOnlySpan<Char>)

將指定的字串附加至檔案,如果檔案不存在,則會建立檔案。

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

參數

path
String

要附加的檔案。

contents
ReadOnlySpan<Char>

要寫入檔案的字元。

例外狀況

path null

path 是空的。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

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

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

path 指定唯讀的檔案。

-或-

path 指定隱藏的檔案。

-或-

path 指定目錄。

-或-

呼叫端沒有必要的許可權。

-或-

目前平臺不支援此作業。

path 格式無效。

備註

指定字串和檔案路徑時,這個方法會開啟指定的檔案,使用指定的編碼將字串附加至檔案結尾,

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

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

適用於

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

要附加至檔案的字串。

例外狀況

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

path null

指定的路徑、檔名或兩者都超過系統定義的最大長度。

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

開啟檔案時發生 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, ReadOnlySpan<Char>, Encoding)

將指定的字串附加至檔案,如果檔案不存在,則會建立檔案。

public:
 static void AppendAllText(System::String ^ path, ReadOnlySpan<char> contents, System::Text::Encoding ^ encoding);
public static void AppendAllText (string path, ReadOnlySpan<char> contents, System.Text.Encoding encoding);
static member AppendAllText : string * ReadOnlySpan<char> * System.Text.Encoding -> unit
Public Shared Sub AppendAllText (path As String, contents As ReadOnlySpan(Of Char), encoding As Encoding)

參數

path
String

要附加的檔案。

contents
ReadOnlySpan<Char>

要寫入檔案的字元。

encoding
Encoding

要套用至字串的編碼方式。

例外狀況

pathencodingnull

path 是空的。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

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

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

path 指定唯讀的檔案。

-或-

path 指定隱藏的檔案。

-或-

path 指定目錄。

-或-

呼叫端沒有必要的許可權。

-或-

目前平臺不支援此作業。

path 格式無效。

備註

指定字串和檔案路徑時,這個方法會開啟指定的檔案,使用指定的編碼將字串附加至檔案結尾,

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

如果不存在,但不會建立新的目錄,則為 。 因此,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

要使用的字元編碼。

例外狀況

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

path null

指定的路徑、檔名或兩者都超過系統定義的最大長度。

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

開啟檔案時發生 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 參數的值必須包含現有的目錄。

適用於