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 ()
public StreamWriter AppendText ()
public function AppendText () : StreamWriter

Return Value

A new StreamWriter.

The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

Create a text file.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

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

using System;
using System.IO;

class Test 
{
	
    public static void Main() 
    {
        FileInfo fi = new FileInfo(@"c:\temp\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);
            }
        }
    }
}

import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        FileInfo fi = new FileInfo("c:\\temp\\MyTest.txt");

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

        // 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 {
            sw.Dispose();
        }

        //Open the file to read from.
        StreamReader sr = fi.OpenText();
        try {
            String s = "";
            while ((s = sr.ReadLine()) != null) {
                Console.WriteLine(s);
            }
        }
        finally {
            sr.Dispose();
        }
    } //main
} //Test

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 System;
using System.IO;

public class AppendTextTest 
{
    public static void Main() 
    {
        // Create a reference to a file, which might or might not exist.
        // If it does not exist, it is not yet created.
        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() );
    }
}

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft