File.AppendAllText 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将指定的字符串追加到文件中,如果文件还不存在则创建该文件。
重载
AppendAllText(String, String) |
打开一个文件,向其中追加指定的字符串,然后关闭该文件。 如果文件不存在,此方法将创建一个文件,将指定的字符串写入文件,然后关闭该文件。 |
AppendAllText(String, String, Encoding) |
使用指定的编码将指定的字符串追加到文件中,如果文件还不存在则创建该文件。 |
AppendAllText(String, String)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- 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 和 .NET Core 版本早于 2.1: 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, String, Encoding)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- 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 和 .NET Core 版本早于 2.1: 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
必须包含现有目录。
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈