File.AppendAllLines(String, IEnumerable<String>, Encoding) Method in C# with Examples
Last Updated :
01 Jun, 2020
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.
CSharp
// 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
Program 2: There is only one file
file.txt has been created whose contents are shown below:
CSharp
// 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:
Similar Reads
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.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.WriteAllLines(String, IEnumerable<String>, Encoding) Method in C# with Examples File.WriteAllLines(String, IEnumerable<String>, 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.Colle
3 min read
File.WriteAllLines(String, IEnumerable<String>) Method in C# with Examples File.WriteAllLines(String, IEnumerable<String>) 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<String>co
3 min read
File.AppendAllText(String, String) Method in C# with Examples 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);
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.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 parameter
2 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
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.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); P
3 min read