File.AppendAllText 方法

定义

将指定的字符串追加到该文件,如果该文件尚不存在,则创建该文件。

重载

AppendAllText(String, ReadOnlySpan<Char>)

将指定的字符串追加到该文件,如果该文件尚不存在,则创建该文件。

AppendAllText(String, String)

打开一个文件,将指定的字符串追加到该文件,然后关闭该文件。 如果文件不存在,此方法将创建一个文件,将指定的字符串写入该文件,然后关闭该文件。

AppendAllText(String, ReadOnlySpan<Char>, Encoding)

将指定的字符串追加到该文件,如果该文件尚不存在,则创建该文件。

AppendAllText(String, String, Encoding)

使用指定的编码将指定的字符串追加到文件,如果该文件尚不存在,则创建该文件。

AppendAllText(String, ReadOnlySpan<Char>)

将指定的字符串追加到该文件,如果该文件尚不存在,则创建该文件。

C#
public static void AppendAllText (string path, ReadOnlySpan<char> contents);

参数

path
String

要追加到的文件。

contents
ReadOnlySpan<Char>

要写入文件的字符。

例外

path null

path 为空。

指定的路径、文件名或两者都超过了系统定义的最大长度。

指定的路径无效(例如,它位于未映射的驱动器上)。

打开文件时出现 I/O 错误。

path 指定了只读文件。

-或-

path 指定了隐藏的文件。

-或-

path 指定了目录。

-或-

调用方没有所需的权限。

-或-

当前平台上不支持此操作。

path 格式无效。

注解

给定字符串和文件路径后,此方法将打开指定的文件,使用指定的编码将字符串追加到文件的末尾,

然后关闭该文件。 即使引发异常,文件句柄也保证由此方法关闭。 该方法创建文件

如果不存在,则不会创建新目录。 因此,路径参数的值必须包含现有目录。

适用于

.NET 9
产品 版本
.NET 9

AppendAllText(String, String)

Source:
File.cs
Source:
File.cs
Source:
File.cs

打开一个文件,将指定的字符串追加到该文件,然后关闭该文件。 如果文件不存在,此方法将创建一个文件,将指定的字符串写入该文件,然后关闭该文件。

C#
public static void AppendAllText (string path, string contents);
C#
public static void AppendAllText (string path, string? contents);

参数

path
String

要向其追加指定字符串的文件。

contents
String

要追加到文件的字符串。

例外

低于 2.1 的 .NET Framework 和 .NET Core 版本:path 为零长度的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用 GetInvalidPathChars() 方法查询无效字符。

path null

指定的路径、文件名或两者都超过了系统定义的最大长度。

指定的路径无效(例如,目录不存在或位于未映射的驱动器上)。

打开文件时出现 I/O 错误。

path 指定了只读文件。

-或-

当前平台上不支持此操作。

-或-

path 指定了目录。

-或-

调用方没有所需的权限。

path 格式无效。

调用方没有所需的权限。

示例

下面的代码示例演示如何使用 AppendAllText 方法向文件末尾添加额外的文本。 在此示例中,如果文件尚不存在,则会创建一个文件,并将文本添加到其中。 但是,驱动器 C 上名为 temp 的目录必须存在,才能使示例成功完成。

C#
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);
    }
}

注解

给定字符串和文件路径后,此方法将打开指定的文件,将字符串追加到文件末尾,然后关闭该文件。 即使引发异常,文件句柄也保证由此方法关闭。

如果文件不存在,该方法将创建该文件,但不会创建新目录。 因此,path 参数的值必须包含现有目录。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

AppendAllText(String, ReadOnlySpan<Char>, Encoding)

将指定的字符串追加到该文件,如果该文件尚不存在,则创建该文件。

C#
public static void AppendAllText (string path, ReadOnlySpan<char> contents, System.Text.Encoding encoding);

参数

path
String

要追加到的文件。

contents
ReadOnlySpan<Char>

要写入文件的字符。

encoding
Encoding

要应用于字符串的编码。

例外

pathencodingnull

path 为空。

指定的路径、文件名或两者都超过了系统定义的最大长度。

指定的路径无效(例如,它位于未映射的驱动器上)。

打开文件时出现 I/O 错误。

path 指定了只读文件。

-或-

path 指定了隐藏的文件。

-或-

path 指定了目录。

-或-

调用方没有所需的权限。

-或-

当前平台上不支持此操作。

path 格式无效。

注解

给定字符串和文件路径后,此方法将打开指定的文件,使用指定的编码将字符串追加到文件的末尾,

然后关闭该文件。 即使引发异常,文件句柄也保证由此方法关闭。 该方法创建文件

如果不存在,则不会创建新目录。 因此,路径参数的值必须包含现有目录。

适用于

.NET 9
产品 版本
.NET 9

AppendAllText(String, String, Encoding)

Source:
File.cs
Source:
File.cs
Source:
File.cs

使用指定的编码将指定的字符串追加到文件,如果该文件尚不存在,则创建该文件。

C#
public static void AppendAllText (string path, string contents, System.Text.Encoding encoding);
C#
public static void AppendAllText (string path, string? contents, System.Text.Encoding 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 的目录必须存在,才能使示例成功完成。

C#
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);
    }
}

注解

给定字符串和文件路径后,此方法将打开指定的文件,使用指定的编码将字符串追加到文件的末尾,然后关闭该文件。 即使引发异常,文件句柄也保证由此方法关闭。

如果文件不存在,该方法将创建该文件,但不会创建新目录。 因此,path 参数的值必须包含现有目录。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0