Open In App

File.AppendText() Method in C# with Examples

Last Updated : 20 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 function accepts a parameter which is illustrated below: 
 

  • path: This is the file where UTF-8 encoded texts are going to be appended. The file is created if it doesn’t already exist.

Exceptions 
 

  • UnauthorizedAccessException: The caller does not have the required permission.
  • ArgumentException: The path is a zero-length string, contains only white space, or contains 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.
  • NotSupportedException: The path is in an invalid format.

Return Value: Returns a stream writer that appends specified UTF-8 encoded texts to the specified file or to a new file.
Below are the programs to illustrate the File.AppendText() method.
Program 1: Before running the below code, a file file.txt is created with some contents which is shown below:
 

file.txt

 

CSharp




// C# program to illustrate the usage
// of File.AppendText() method
 
// Using System, System.IO namespaces
using System;
using System.IO;
 
class GFG {
    // Main method
    public static void Main()
    {
        // Creating a file
        string myfile = @"file.txt";
 
        // Appending the given texts
        using(StreamWriter sw = File.AppendText(myfile))
        {
            sw.WriteLine("Gfg");
            sw.WriteLine("GFG");
            sw.WriteLine("GeeksforGeeks");
        }
 
        // Opening the file for reading
        using(StreamReader sr = File.OpenText(myfile))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) {
                Console.WriteLine(s);
            }
        }
    }
}


Executing: 
 

mcs -out:main.exe main.cs
mono main.exe
Geeks
Gfg
GFG
GeeksforGeeks

After running the above code, above output is shown and the existing file file.txt becomes like below: 
 

Program 2: Initially, no file is created and hence below code itself create a file named as file.txt
 

CSharp




// C# program to illustrate the usage
// of File.AppendText() method
 
// Using System, System.IO namespaces
using System;
using System.IO;
 
class GFG {
    // Main method
    public static void Main()
    {
        // Creating a file
        string myfile = @"file.txt";
 
        // Checking the above file
        if (!File.Exists(myfile)) {
            // Creating the same file if it doesn't exist
            using(StreamWriter sw = File.CreateText(myfile))
            {
                sw.WriteLine("GeeksforGeeks");
                sw.WriteLine("is");
                sw.WriteLine("a");
            }
        }
 
        // Appending the given texts
        using(StreamWriter sw = File.AppendText(myfile))
        {
            sw.WriteLine("computer");
            sw.WriteLine("science");
            sw.WriteLine("portal.");
        }
 
        // Opening the file for reading
        using(StreamReader sr = File.OpenText(myfile))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) {
                Console.WriteLine(s);
            }
        }
    }
}


Executing: 
 

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

After running the above code, a new file file.txt is created which is shown below:
 

file.txt

 



Similar Reads

C# Program To Copy Content Of One File To Another File By Overwriting Same File Name
Given a file, now our task is to copy data from one file to another file by overwriting the same file name using C#. So we use the following methods to perform this task: 1. Copy(String, String, Boolean): It is used to copy the content of one file to a new file with overwrite. Syntax: File.Copy(Myfile1, Myfile2, owrite); Where Myfile1 is the first
2 min read
File.GetLastWriteTimeUtc() Method in C# with Examples
File.GetLastWriteTimeUtc(String) is an inbuilt File class method which is used to return the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.Syntax: public static DateTime GetLastWriteTimeUtc (string path); Parameter: This function accepts a parameter which is illustrated below: path: Thi
2 min read
File.Create(String, Int32, FileOptions, FileSecurity) Method in C# with Examples
File.Create(String, Int32, FileOptions, FileSecurity) is an inbuilt File class method which is used to overwrite an existing file, specifying a buffer size and options that describe how to create or overwrite the file and value that determines the access control and audit security for the file. If the specified file is not existing, this function i
3 min read
File.Open(String, FileMode, FileAccess) Method in C# with Examples
File.Open(String, FileMode, FileAccess) is an inbuilt File class method that is used to open a FileStream on the specified path with the specified mode and access with no sharing.Syntax: public static System.IO.FileStream Open (string path, System.IO.FileMode mode, System.IO.FileAccess access); Parameter: This function accepts three parameters whic
3 min read
File.Open(String, FileMode, FileAccess, FileShare) Method in C# with Examples
File.Open(String, FileMode, FileAccess, FileShare) is an inbuilt File class method that is used to open a FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.Syntax: public static System.IO.FileStream Open (string path, System.IO.FileMode mode, System.IO.FileAccess acce
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 accepts two parameters which are illustrated below: sourc
3 min read
File.Move() Method in C# with Examples
File.Move() is an inbuilt File class method that is used to move a specified file to a new location. This method also provides the option to specify a new file name. Syntax: public static void Move (string sourceFileName, string destFileName); Parameter: This function accepts two parameters which are illustrated below: sourceFileName: This is the s
2 min read
File.Exists() Method in C# with Examples
File.Exists(String) is an inbuilt File class method that is used to determine whether the specified file exists or not. This method returns true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. Also, if the path is null, then this method returns false. Syntax: public static bool Exists (st
2 min read
File.CreateText() Method in C# with Examples
File.CreateText() is an inbuilt File class method that is used to overwrite the contents of the existing file with the given UTF-8 encoded text and if the file is not created already, this function will create a new file with the specified contents. Syntax: public static System.IO.StreamWriter CreateText (string path); Parameter: This function acce
3 min read
File.Delete() Method in C# with Examples
File.Delete(String) is an inbuilt File class method which is used to delete the specified file.Syntax: public static void Delete (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the specified file path which is to be deleted. Exceptions: ArgumentException: The path is a zero-length string, contai
3 min read
Article Tags :