C# File AppendAllText(String, String)

Description

File AppendAllText(String, String) Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.

Syntax

File.AppendAllText(String, String) has the following syntax.


public static void AppendAllText(
  string path,
  string contents
)

Parameters

File.AppendAllText(String, String) has the following parameters.

  • path - The file to append the specified string to.
  • contents - The string to append to the file.

Returns

File.AppendAllText(String, String) method returns

Example

The following code example demonstrates the use of the AppendAllText method to add extra text to the end of a file.


using System;/*ww w  .  j a v  a2 s . co  m*/
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";
        string createText = "Hello and Welcome" + Environment.NewLine;
         File.WriteAllText(path, createText);

        string appendText = "This is from java2s.com" + Environment.NewLine;
        File.AppendAllText(path, appendText);

        string readText = File.ReadAllText(path);
        Console.WriteLine(readText);
    }
}




















Home »
  C# Tutorial »
    System.IO »




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