File.AppendAllText 方法

定义

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

重载

AppendAllText(String, String)

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

AppendAllText(String, String, Encoding)

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

AppendAllText(String, String)

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

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 和 .net Core 版本早于2.1: path 是一个长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。

pathnull

指定的路径和/或文件名超过了系统定义的最大长度。

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

打开文件时发生 I/O 错误。

path 指定了一个只读文件。

  • 或 - 当前平台不支持此操作。

  • 或 - path 指定了一个目录。

  • 或 - 调用方没有所要求的权限。

path 的格式无效。

调用方没有所要求的权限。

示例

下面的代码示例演示如何使用 AppendAllText 方法将额外文本添加到文件末尾。 在此示例中,如果文件尚不存在,则将创建一个文件,并向其中添加文本。 但是, 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);
    }
}
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)

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

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 和 .net Core 版本早于2.1: path 是一个长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。

pathnull

指定的路径和/或文件名超过了系统定义的最大长度。

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

打开文件时发生 I/O 错误。

path 指定了一个只读文件。

  • 或 - 当前平台不支持此操作。

  • 或 - path 指定了一个目录。

  • 或 - 调用方没有所要求的权限。

path 的格式无效。

调用方没有所要求的权限。

示例

下面的代码示例演示如何使用 AppendAllText 方法将额外文本添加到文件末尾。 在此示例中,如果文件尚不存在,则将创建一个文件,并向其中添加文本。 但是, 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);
    }
}
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 必须包含现有目录。

适用于