Open In App

File.AppendAllText(String, String, Encoding) Method in C# with Examples

File.AppendAllText(String, String, Encoding) is an inbuilt File class method which is used to append the specified string to the given file using the specified encoding if that file exists else creates a new file and then appending is done.
 

Syntax:  



public static void AppendAllText (string path, string contents, System.Text.Encoding encoding);

Parameter: This function accepts two parameters which are illustrated below:
 

  • path: This is the file where given contents are going to be appended.
  • contents: This is the specified contents which is to be appended to the file.
  • Encoding: This is the specified character encoding.

Exceptions:
 



Below are the programs to illustrate the File.AppendAllText(String, String, Encoding) method.
Program 1: Before running the below code, a file is created with some contents shown below:
 

 




// C# program to illustrate the usage
// of File.AppendAllText() method
 
// Using System, System.IO,
// System.Text and System.Linq namespaces
using System;
using System.IO;
using System.Text;
using System.Linq;
 
class GFG {
    // Main() method
    public static void Main()
    {
        // Creating a file
        string myfile = @"file.txt";
 
        // Adding extra texts
        string appendText = " is a CS portal." + Environment.NewLine;
        File.AppendAllText(myfile, appendText, Encoding.UTF8);
 
        // Opening the file to read from.
        string readText = File.ReadAllText(myfile);
        Console.WriteLine(readText);
    }
}

Executing: 
 

mcs -out:main.exe main.cs
mono main.exe
GeeksforGeeks is a CS portal.

After running the above code, above output is shown and the file contents become like shown below:
 

Program 2: Initially no file is created but the below code itself creates a new file and appends the specified contents.
 




// C# program to illustrate the usage
// of File.AppendAllText() method
 
// Using System, System.IO,
// System.Text and System.Linq namespaces
using System;
using System.IO;
using System.Text;
using System.Linq;
 
class GFG {
    // Main() method
    public static void Main()
    {
        // Creating a file
        string myfile = @"file.txt";
 
        // Checking the existence of file
        if (!File.Exists(myfile)) {
            // Creating a file with below content
            string createText = "GFG" + Environment.NewLine;
            File.WriteAllText(myfile, createText);
        }
 
        // Adding extra texts
        string appendText = " is a CS portal." + Environment.NewLine;
        File.AppendAllText(myfile, appendText, Encoding.UTF8);
 
        // Opening the file to read from.
        string readText = File.ReadAllText(myfile);
        Console.WriteLine(readText);
    }
}

Executing: 
 

mcs -out:main.exe main.cs
mono main.exe
GFG
 is a CS portal.

After running the above code, above output has been shown and a new file created which is shown below:
 

 


Article Tags :
C#