Skip to main content
.NET Framework Class Library
FileInfoAppendText Method

Creates a StreamWriter that appends text to the file represented by this instance of the FileInfo.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)
Syntax
Public Function AppendText As [%$TOPIC/7148c58b_en-us_VS_110_3_0_0_0_0%]
public [%$TOPIC/7148c58b_en-us_VS_110_3_0_1_0_0%] AppendText()
public:
[%$TOPIC/7148c58b_en-us_VS_110_3_0_2_0_0%]^ AppendText()
member AppendText : unit -> [%$TOPIC/7148c58b_en-us_VS_110_3_0_3_0_0%]

Return Value

Type: System.IOStreamWriter
A new StreamWriter.
Examples

The following example appends text to a file and reads from the file.

Imports System
Imports System.IO

Public Class Test

    Public Shared Sub Main()
        Dim fi As FileInfo = New FileInfo("c:\temp\MyTest.txt")
        Dim sw As StreamWriter

        ' This text is added only once to the file. 
        If fi.Exists = False Then 
            'Create a file to write to.
            sw = fi.CreateText()
            sw.WriteLine("Hello")
            sw.WriteLine("And")
            sw.WriteLine("Welcome")
            sw.Flush()
            sw.Close()
        End If 

        ' This text will always be added, making the file longer over time 
        ' if it is not deleted.
        sw = fi.AppendText()

        sw.WriteLine("This")
        sw.WriteLine("is Extra")
        sw.WriteLine("Text")
        sw.Flush()
        sw.Close()

        'Open the file to read from. 
        Dim sr As StreamReader = fi.OpenText()
        Dim s As String 
        Do While sr.Peek() >= 0
            s = sr.ReadLine()
            Console.WriteLine(s)
        Loop
        sr.Close()
    End Sub 
End Class 
'This code produces output similar to the following;  
'results may vary based on the computer/file structure/etc.: 
' 
'Hello 
'And 
'Welcome 
'This 
'is Extra 
'Text 

'When you run this application a second time, you will see the following output: 
' 
'Hello 
'And 
'Welcome 
'This 
'is Extra 
'Text 
'This 
'is Extra 
'Text
using System;
using System.IO;

class Test 
{
	
    public static void Main() 
    {
        FileInfo fi = new FileInfo(@"c:\MyTest.txt");

        // This text is added only once to the file. 
        if (!fi.Exists) 
        {
            //Create a file to write to. 
            using (StreamWriter sw = fi.CreateText()) 
            {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }	
        }

        // This text will always be added, making the file longer over time 
        // if it is not deleted. 
        using (StreamWriter sw = fi.AppendText()) 
        {
            sw.WriteLine("This");
            sw.WriteLine("is Extra");
            sw.WriteLine("Text");
        }	

        //Open the file to read from. 
        using (StreamReader sr = fi.OpenText()) 
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) 
            {
                Console.WriteLine(s);
            }
        }
    }
}
//This code produces output similar to the following;  
//results may vary based on the computer/file structure/etc.: 
// 
//Hello 
//And 
//Welcome 
//This 
//is Extra 
//Text 

//When you run this application a second time, you will see the following output: 
// 
//Hello 
//And 
//Welcome 
//This 
//is Extra 
//Text 
//This 
//is Extra 
//Text
using namespace System;
using namespace System::IO;

int main()
{
   FileInfo^ fi = gcnew FileInfo( "c:\\MyTest.txt" );

   // This text is added only once to the file. 
   if (  !fi->Exists )
   {
      //Create a file to write to.
      StreamWriter^ sw = fi->CreateText();
      try
      {
         sw->WriteLine( "Hello" );
         sw->WriteLine( "And" );
         sw->WriteLine( "Welcome" );
      }
      finally
      {
         if ( sw )
            delete (IDisposable^)sw;
      }
   }

   // This text will always be added, making the file longer over time 
   // if it is not deleted.
   StreamWriter^ sw = fi->AppendText();
   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 = fi->OpenText();
   try
   {
      String^ s = "";
      while ( s = sr->ReadLine() )
      {
         Console::WriteLine( s );
      }
   }
   finally
   {
      if ( sr )
         delete (IDisposable^)sr;
   }
}
//This code produces output similar to the following;  
//results may vary based on the computer/file structure/etc.: 
// 
//Hello 
//And 
//Welcome 
//This 
//is Extra 
//Text 
// 
//When you run this application a second time, you will see the following output: 
// 
//Hello 
//And 
//Welcome 
//This 
//is Extra 
//Text 
//This 
//is Extra 
//Text

The following example demonstrates appending text to the end of a file and also displays the result of the append operation to the console. The first time this routine is called, the file is created if it does not exist. After that, the specified text is appended to the file.

Imports System
Imports System.IO

Public Class AppendTextTest
    Public Shared Sub Main()
        Dim fi As New FileInfo("temp.txt")
        Dim sw As StreamWriter = fi.AppendText()
        sw.WriteLine("Add as many lines as you like...")
        sw.WriteLine("Add another line to the output...")
        sw.Flush()
        sw.Close()
        Dim sr As New StreamReader(fi.OpenRead())
        ' Get the information out of the file and display it. 
        ' Remember that the file might have other lines if it already existed. 
        While sr.Peek() <> -1
            Console.WriteLine(sr.ReadLine())
        End While 
    End Sub 'Main
End Class 'AppendTextTest
'This code produces output similar to the following; 
'results may vary based on the computer/file structure/etc.: 
'Add as many lines as you like... 
'Add another line to the output...
using System;
using System.IO;

public class AppendTextTest 
{
    public static void Main() 
    {
        FileInfo fi = new FileInfo("temp.txt");
        // Create a writer, ready to add entries to the file.
        StreamWriter sw = fi.AppendText();
        sw.WriteLine("Add as many lines as you like...");
        sw.WriteLine("Add another line to the output...");
        sw.Flush();
        sw.Close();
        // Get the information out of the file and display it. 
        // Remember that the file might have other lines if it already existed.
        StreamReader sr = new StreamReader(fi.OpenRead());
        while (sr.Peek() != -1)
            Console.WriteLine( sr.ReadLine() );
    }
}
//This code produces output similar to the following;  
//results may vary based on the computer/file structure/etc.: 
//Add as many lines as you like... 
//Add another line to the output...
using namespace System;
using namespace System::IO;
int main()
{

   FileInfo^ fi = gcnew FileInfo( "temp.txt" );

   // Create a writer, ready to add entries to the file.
   StreamWriter^ sw = fi->AppendText();
   sw->WriteLine( "Add as many lines as you like..." );
   sw->WriteLine( "Add another line to the output..." );
   sw->Flush();
   sw->Close();

   // Get the information out of the file and display it. 
   // Remember that the file might have other lines if it already existed.
   StreamReader^ sr = gcnew StreamReader( fi->OpenRead() );
   while ( sr->Peek() != -1 )
      Console::WriteLine( sr->ReadLine() );
//This code produces output similar to the following; 
//results may vary based on the computer/file structure/etc.: 
//Add as many lines as you like... 
//Add another line to the output...

}
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
.NET Framework Security
Platforms

Windows Phone 8.1, Windows Phone 8, 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)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.