C# File AppendAllText(String, String, Encoding)

Description

File AppendAllText(String, String, Encoding) Appends the specified string to the file, creating the file if it does not already exist.

Syntax

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


public static void AppendAllText(
  string path,//from  w  w  w  . j a v a  2s.  c  o  m
  string contents,
  Encoding encoding
)

Parameters

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

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

Returns

File.AppendAllText(String, String, Encoding) 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;//w  w  w  .  j  a v  a 2  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, Encoding.UTF8);

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

        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