C# FileInfo AppendText

Description

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

Syntax

FileInfo.AppendText has the following syntax.


public StreamWriter AppendText()

Returns

FileInfo.AppendText method returns A new StreamWriter.

Example

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


/* w w  w.  ja  v a  2 s .  co  m*/
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        FileInfo fi = new FileInfo(@"c:\MyTest.txt");
        using (StreamWriter sw = fi.CreateText()) {
           sw.WriteLine("Hello");
           sw.WriteLine("And");
           sw.WriteLine("Welcome");
        }
        using (StreamWriter sw = fi.AppendText()) 
        {
            sw.WriteLine("This");
            sw.WriteLine(" is from ");
            sw.WriteLine("java2s.com");
        }  
        using (StreamReader sr = fi.OpenText()) 
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) 
            {
                Console.WriteLine(s);
            }
        }
    }
}




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter