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.
Assembly: mscorlib (in mscorlib.dll)
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 Nothing.
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" 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. sw = File.CreateText(path) sw.WriteLine("Hello") sw.WriteLine("And") sw.WriteLine("Welcome") sw.Flush() sw.Close() End If ' This text is always added, making the file longer over time ' if it is not deleted. sw = File.AppendText(path) sw.WriteLine("This") sw.WriteLine("is Extra") sw.WriteLine("Text") sw.Flush() sw.Close() ' Open the file to read from. Dim sr As StreamReader = File.OpenText(path) Dim s As String Do While sr.Peek() >= 0 s = sr.ReadLine() Console.WriteLine(s) Loop sr.Close() End Sub End Class
Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)