File.AppendText(String) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Creates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist.
public:
static System::IO::StreamWriter ^ AppendText(System::String ^ path);
public static System.IO.StreamWriter AppendText (string path);
static member AppendText : string -> System.IO.StreamWriter
Public Shared Function AppendText (path As String) As StreamWriter
Parameters
- path
- String
The path to the file to append to.
Returns
A stream writer that appends UTF-8 encoded text to the specified file or to a new file.
Exceptions
The caller does not have the required permission.
.NET Framework and .NET Core versions older than 2.1: path
is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method.
path
is null
.
The specified path, file name, or both exceed the system-defined maximum length.
The specified path is invalid (for example, the directory doesn't exist or it is on an unmapped drive).
path
is in an invalid format.
Examples
The following example appends text to a file. The method creates a new file if the file doesn't exist. However, the directory named temp
on drive C must exist for the example to complete successfully.
using namespace System;
using namespace System::IO;
int 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.
StreamWriter^ sw = File::CreateText( path );
try
{
sw->WriteLine( "Hello" );
sw->WriteLine( "And" );
sw->WriteLine( "Welcome" );
}
finally
{
if ( sw )
delete (IDisposable^)sw;
}
}
// This text is always added, making the file longer over time
// if it is not deleted.
StreamWriter^ sw = File::AppendText( path );
try
{
sw->WriteLine( "This" );
sw->WriteLine( "is Extra" );
sw->WriteLine( "Text" );
}
finally
{
if ( sw )
delete (IDisposable^)sw;
}
// Open the file to read from.
StreamReader^ sr = File::OpenText( path );
try
{
String^ s = "";
while ( s = sr->ReadLine() )
{
Console::WriteLine( s );
}
}
finally
{
if ( sr )
delete (IDisposable^)sr;
}
}
using System;
using System.IO;
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.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
// This text is always added, making the file longer over time
// if it is not deleted.
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine("This");
sw.WriteLine("is Extra");
sw.WriteLine("Text");
}
// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
}
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.
use sw = File.CreateText path
sw.WriteLine "Hello"
sw.WriteLine "And"
sw.WriteLine "Welcome"
// This text is always added, making the file longer over time
// if it is not deleted.
do
use sw = File.AppendText path
sw.WriteLine "This"
sw.WriteLine "is Extra"
sw.WriteLine "Text"
// Open the file to read from.
do
use sr = File.OpenText path
let mutable s = sr.ReadLine()
while isNull s |> not do
printfn $"{s}"
s <- sr.ReadLine()
Imports System.IO
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 Not File.Exists(path) Then
' Create a file to write to.
Using sw As StreamWriter = File.CreateText(path)
sw.WriteLine("Hello")
sw.WriteLine("And")
sw.WriteLine("Welcome")
End Using
End If
' This text is always added, making the file longer over time
' if it is not deleted.
Using sw As StreamWriter = File.AppendText(path)
sw.WriteLine("This")
sw.WriteLine("is Extra")
sw.WriteLine("Text")
End Using
' Open the file to read from.
Using sr As StreamReader = File.OpenText(path)
Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
Loop
End Using
End Sub
End Class
Remarks
This method is equivalent to the StreamWriter(String, Boolean) constructor overload. If the file specified by path
does not exist, it is created. If the file does exist, write operations to the StreamWriter append text to the file. Additional threads are permitted to read the file while it is open.
The path
parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.
The path
parameter is not case-sensitive.
For a list of common I/O tasks, see Common I/O Tasks.