File.AppendText Method (String)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

Public Shared Function AppendText (
	path As String
) As StreamWriter


Type: System.String

The path to the file to append to.

Return Value

Type: System.IO.StreamWriter

A stream writer that appends UTF-8 encoded text to the specified file or to a new file.

Exception Condition

The caller does not have the required permission.


path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.


path is null.


The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.


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.

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.

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.

Imports System
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)
      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("is Extra")
    End Using

    ' Open the file to read from. 
    Using sr As StreamReader = File.OpenText(path)
      Do While sr.Peek() >= 0
    End Using

  End Sub
End Class


for appending to the specified file. Associated enumeration: FileIOPermissionAccess.Append

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top