Open In App

File.AppendAllLines(String, IEnumerable<String>, Encoding) Method in C# with Examples

Last Updated : 01 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

File.AppendAllLines(String, IEnumerable<String>, Encoding) is an inbuilt File class method which is used to append specified lines to a file by using a specified encoding and then closes the file. If the specified file does not exist, this method creates a new file, writes the specified lines to the file, and then closes the file.

Syntax:

public static void AppendAllLines (string path, System.Collections.Generic.IEnumerable<String> contents, System.Text.Encoding encoding);

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

  • path: This is the file where lines are going to be appended. The file is created if it doesn’t already exist.
  • contents: This is the specified contents which is to be appended to the file.
  • encoding: This is the specified character encoding.

Exceptions:

  • ArgumentException: The path is a zero-length string, contains only white space, or one more invalid characters defined by the GetInvalidPathChars() method.
  • ArgumentNullException: Either path, contents, or encoding is null.
  • DirectoryNotFoundException: The path is invalid i.e, the directory doesn’t exist or it is on an unmapped drive.
  • FileNotFoundException: The file given by the path was not found.
  • IOException: An I/O error occurred while opening the file.
  • PathTooLongException: The path exceeds the system-defined maximum length.
  • NotSupportedException: The path is in an invalid format.
  • SecurityException: The caller does not have the required permission.
  • UnauthorizedAccessException: The path specifies a file that is read-only. OR This operation is not supported on the current platform. OR the path is a directory. OR the caller does not have the required permission.

Below are the programs to illustrate the File.AppendAllLines(String, IEnumerable, Encoding) method.

Program 1: There is two files used one is file.txt and another one is gfg.txt whose contents are shown below before running the program.

file.txt

gfg.txt




// C# program to illustrate the usage
// of File.AppendAllLines() method
   
// Using System, System.IO,
// System.Linq and System.Text namespaces
using System;
using System.IO;
using System.Linq;
using System.Text;
   
// Creating class
class GfG {
   
    // Creating a file
    static string myfile = @"file.txt";
   
    // Main method
    static void Main(string[] args)
    {
   
        // Reading lines of the file created above
        var appendTofile = from line in File.ReadLines(myfile)
   
       // Using select statement
       select line;
   
        // Calling AppendAllLines() method with its
        // parameters
        File.AppendAllLines(@"gfg.txt", appendTofile, Encoding.UTF8);
   
        // Printed when the stated file is appended
        Console.WriteLine("All lines are appended");
    }
}


Executing:

mcs -out:main.exe main.cs
mono main.exe
All lines are appended

After running the above code, above output will be shown and content of the file gfg.txt will be like shown below, that means contents of file.txt have been appended to the file gfg.txt

gfg.txt

Program 2: There is only one file file.txt has been created whose contents are shown below:

file.txt




// C# program to illustrate the usage
// of File.AppendAllLines() method
   
// Using System, System.IO,
// System.Linq and System.Text namespaces
using System;
using System.IO;
using System.Linq;
using System.Text;
   
// Creating class
class GfG {
   
    // Creating a file
    static string myfile = @"file.txt";
   
    // Main method
    static void Main(string[] args)
    {
   
        // Reading lines of the file created above
        var appendTofile = from line in File.ReadLines(myfile)
   
       // It only appends the line that starts with g
       where(line.StartsWith("g"))
  
       // Using select statement
       select line;
   
        // Calling AppendAllLines() method with its
        // parameters
        File.AppendAllLines(@"gfg.txt", appendTofile, Encoding.UTF8);
   
        // Printed when the stated file is appended
        Console.WriteLine("All lines are appended");
    }
}


Executing:

mcs -out:main.exe main.cs
mono main.exe
All lines are appended

After running the above code, above output will be shown and it will create a new file called gfg.txt having contents same as file file.txt:

gfg.txt



Similar Reads

File.AppendAllLines(String, IEnumerable&lt;String&gt;) Method in C# with Examples
File.AppendAllLines(String, IEnumerable&lt;String&gt;) 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&lt;String&gt; contents); Parameter: This function accepts two parameters which are illustrat
3 min read
File.WriteAllLines(String, IEnumerable&lt;String&gt;, Encoding) Method in C# with Examples
File.WriteAllLines(String, IEnumerable&lt;String&gt;, Encoding) is an inbuilt File class method that is used to create a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file. Syntax: public static void WriteAllLines (string path, System.Collections.Generic.IEnumerable&lt;String&gt; contents,
3 min read
File.WriteAllLines(String, IEnumerable&lt;String&gt;) Method in C# with Examples
File.WriteAllLines(String, IEnumerable&lt;String&gt;) is an inbuilt File class method that is used to create a new file, writes a collection of strings to the file, and then closes the file. Syntax: public static void WriteAllLines (string path, System.Collections.Generic.IEnumerable&lt;String&gt;contents); Parameter: This function accepts two para
3 min read
C# Program For Implementing IEnumerable Interface Using LINQ
LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives the ability to .NET languages to generate queries to retrieve data from the data source. It removes the mismatch between programming languages and databases and the syntax used to create a query is the same no matter which type of data source is used. In this arti
3 min read
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); Param
3 min read
File.WriteAllText(String, String, Encoding) Method in C# with Examples
File.WriteAllText(String, String, Encoding) is an inbuilt File class method that is used to create a new file, writes the specified string to the file using the specified encoding, and then closes the file. If the target file already exists, it is overwritten. Syntax: public static void WriteAllText (string path, string contents, System.Text.Encodi
3 min read
File.WriteAllLines(String, String[], Encoding) Method in C# with Examples
File.WriteAllLines(String, String[], Encoding) is an inbuilt File class method that is used to create a new file, writes the specified string array to the file by using the specified encoding, and then closes the file.Syntax: public static void WriteAllLines (string path, string[] contents, System.Text.Encoding encoding); Parameter: This function a
3 min read
File.ReadAllLines(String, Encoding) Method in C# with Examples
File.ReadAllLines(String, Encoding) is an inbuilt File class method that is used to open a text file then reads all lines of the file into a string array with the specified encoding and then closes the file.Syntax: public static string[] ReadAllLines (string path, System.Text.Encoding encoding); Parameter: This function accepts two parameters which
3 min read
File.ReadLines(String, Encoding) Method in C# with Examples
File.ReadLines(String, Encoding) is an inbuilt File class method that is used to read the lines of a file that has a specified encoding. Syntax: public static System.Collections.Generic.IEnumerable ReadLines (string path, System.Text.Encoding encoding); Parameter: This function accepts two parameters which are illustrated below: path: This is the s
2 min read
File.ReadAllText(String, Encoding) Method in C# with Examples
File.ReadAllText(String, Encoding) is an inbuilt File class method that is used to open a text file then reads all the text in the file with the specified encoding and then closes the file.Syntax: public static string ReadAllText (string path, System.Text.Encoding encoding); Parameter: This function accepts a parameter which is illustrated below: p
2 min read
Article Tags :