Export (0) Print
Expand All

FileInfo::AppendText 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)

public:
StreamWriter^ AppendText()

Return Value

Type: System.IO::StreamWriter
A new StreamWriter.

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

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.

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...

}

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Show:
© 2015 Microsoft