File.AppendAllText(String, String) Method in C# with Examples
Last Updated :
28 Apr, 2025
File.AppendAllText(String, String) is an inbuilt File class method which is used to append the specified string to the given file if that file exists else creates a new file and then appending is done. It also closes the file.
Syntax:
public static void AppendAllText (string path, string contents);
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.
Exceptions:
- ArgumentException: The path is a zero-length string, contains only white space, or one or more invalid characters as defined by InvalidPathChars.
- ArgumentNullException: The path is null.
- PathTooLongException: The given path, file name, or both exceed the system-defined maximum length.
- DirectoryNotFoundException: The given path is invalid i.e, the directory doesn't exist or it is on an unmapped drive.
- IOException: An I/O error occurred while opening the file.
- UnauthorizedAccessException: The path specified a file that is read-only. OR this operation is not supported on the current platform. OR the path specified a directory. OR the caller does not have the required permission.
- NotSupportedException: The path is in an invalid format.
- SecurityException: The caller does not have the required permission.
Below are the programs to illustrate the File.AppendAllText(String, String) method.
Program 1: Before running the below code, a file is created with some contents shown below:
CSharp
// C# program to illustrate the usage
// of File.AppendAllText() method
// Using System, System.IO,
// and System.Text namespaces
using System;
using System.IO;
using System.Text;
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);
// 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.
CSharp
// C# program to illustrate the usage
// of File.AppendAllText() method
// Using System, System.IO,
// and System.Text namespaces
using System;
using System.IO;
using System.Text;
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 contents
string appendText = "is a CS portal." + Environment.NewLine;
File.AppendAllText(myfile, appendText);
// 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:
Similar Reads
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, s
3 min read
File.Copy(String, String) Method in C# with Examples File.Copy(String, String) is an inbuilt File class method that is used to copy the content of the existing source file content to another destination file which is created by this function. Syntax:  public static void Copy (string sourceFileName, string destFileName); Parameter: This function acce
3 min read
File.AppendText() Method in C# with Examples File.AppendText() is an inbuilt File class method which is used to create a StreamWriter that appends UTF-8 encoded text to an existing file else it creates a new file if the specified file does not exist.Syntax:Â Â public static System.IO.StreamWriter AppendText (string path); Parameter: This functi
3 min read
File.Copy(String, String, Boolean) Method in C# with Examples File.Copy(String, String, Boolean) is an inbuilt File class method that is used to copy the content of the existing source file content to another destination file if exist, else create a new destination file then copying process is done.Syntax:Â Â public static void Copy (string sourceFileName, stri
4 min read
File.AppendAllLines(String, IEnumerable<String>) Method in C# with Examples File.AppendAllLines(String, IEnumerable<String>) is an inbuilt File class method which is used to append specified lines to a file and then closes the file. Syntax: public static void AppendAllLines (string path, System.Collections.Generic.IEnumerable<String> contents); Parameter: This f
3 min read
File.Create(String) Method in C# with Examples File.Create(String) is an inbuilt File class method which is used to overwrites an existing file else create a new file if the specified file is not existing. Syntax:Â public static System.IO.FileStream Create (string path); Parameter: This function accepts a parameter which is illustrated below:
3 min read