File: AppendText

AppendText

Creates a StreamWriter that appends UTF-8 encoded text to an existing file.



 Public Function AppendText(
	  ByRef Path As String ) As StreamWriter

Parameters

Path
[ByRef] String. The path to the file to append to.

Return Values

StreamWriter -  A StreamWriter that appends UTF-8 encoded text to an existing file.

Remarks

This method is equivalent to the NewStreamWriter(String, Boolean) constructor. 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.

Exceptions

ExceptionCondition
UnauthorizedAccessException Path is read-only.
-or-
Path is a directory.
ArgumentException Path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by GetInvalidPathChars.
PathTooLongException The specified path, file name, or both exceed the system-defined maximum length. On Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
DirectoryNotFoundException The specified path is invalid (for example, it is on an unmapped drive).
NotSupportedException Path is in an invalid format.

Examples

The following example appends text to a file.

Public Sub Main()
    Const Path As String = "c:\temp\MyTest.txt"
    Dim sw As StreamWriter

    ' This text is added only once to the file.
    If Not File.Exists(Path) Then
        ' Create a file to write to.
        Set sw = File.CreateText(Path)

        sw.WriteLine "Hello"
        sw.WriteLine "And"
        sw.WriteLine "Welcome"
        sw.Flush
        sw.CloseWriter
    End If

    ' This text is always added, making the file longer over time
    ' if it is not deleted.
    Set sw = File.AppendText(Path)
    sw.WriteLine "This"
    sw.WriteLine "is Extra"
    sw.WriteLine "Text"
    sw.Flush
    sw.CloseWriter

    ' Open the file to read from.
    Dim sr As StreamReader
    Dim s As String
    
    Set sr = File.OpenText(Path)
    
    Do While sr.Peek >= 0
        s = sr.ReadLine
        Debug.Print s
    Loop
    
    sr.CloseReader
End Sub

See Also

Project CorLib Overview

Class File Overview

StreamWriter