Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 01 Jun, 2020

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 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.

Exceptions:



  • ArgumentException: The path is a zero-length string, contains only white space, or contains one more invalid characters defined by the GetInvalidPathChars() method.
  • ArgumentNullException: Either path or contents are null.
  • DirectoryNotFoundException: The path is invalid i.e, the directory doesn’t exist or it is on an unmapped drive.
  • FileNotFoundException: The file specified 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 permission to write to the file.
  • 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.

Below are the programs to illustrate the File.AppendAllLines() 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, and
// System.Linq namespaces
using System;
using System.IO;
using System.Linq;
   
// 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);
   
        // 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, and
// System.Linq namespaces
using System;
using System.IO;
using System.Linq;
   
// 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);
   
        // 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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!