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.
using System;
using System.IO;
using System.Linq;
class GfG {
static string myfile = @"file.txt" ;
static void Main( string [] args)
{
var appendTofile = from line in File.ReadLines(myfile)
select line;
File.AppendAllLines( @"gfg.txt" , appendTofile);
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:
using System;
using System.IO;
using System.Linq;
class GfG {
static string myfile = @"file.txt" ;
static void Main( string [] args)
{
var appendTofile = from line in File.ReadLines(myfile)
where (line.StartsWith( "g" ))
select line;
File.AppendAllLines( @"gfg.txt" , appendTofile);
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:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...