File.ReadLines(String) Method in C# with Examples Last Updated : 01 Jun, 2020 Comments Improve Suggest changes Like Article Like Report File.ReadLines(String) is an inbuilt File class method that is used to read the lines of a file. Syntax: public static System.Collections.Generic.IEnumerable ReadLines (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the specified file for reading. Exceptions: ArgumentException: The path is a zero-length string, contains only white space, or one or more invalid characters defined by the GetInvalidPathChars() method. ArgumentNullException: The path is null. DirectoryNotFoundException: The path is invalid. 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. 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. Return Value: Returns all the lines of the specified file. Below are the programs to illustrate the File.ReadLines(String) method. Program 1: Initially, a file file.txt is created with some contents shown below- CSharp // C# program to illustrate the usage // of File.ReadLines(String) method // Using System and System.IO namespaces using System; using System.IO; public class GFG { public static void Main(String[] argv) { // Calling the ReadLines(String) function foreach(string line in File.ReadLines(@"file.txt")) { // Printing the file contents Console.WriteLine(line); } } } Output: GFG gfg Geeks GeeksforGeeks geeksforgeeks Program 2: Initially, a file file.txt is created with some contents shown below- Below code filter some contents from the file and prints them back. CSharp // C# program to illustrate the usage // of File.ReadLines(String) method // Using System and System.IO namespaces using System; using System.IO; public class GFG { public static void Main(String[] argv) { // Calling the ReadLines(String) function foreach(string line in File.ReadLines(@"file.txt")) { // Filtering the file contents and printing if (line.Contains("GFG")) { Console.WriteLine(line); } } } } Output: GFG Comment More infoAdvertise with us Next Article File.ReadLines(String) Method in C# with Examples K Kanchan_Ray Follow Improve Article Tags : C# CSharp-File-Handling Similar Reads File.ReadAllLines(String) Method in C# with Examples File.ReadAllLines(String) 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 and then closes the file.Syntax:Â Â public static string[] ReadAllLines (string path); Parameter: This function accepts a parameter which is illustrated belo 2 min read File.ReadAllText(String) Method in C# with Examples File.ReadAllText(String) is an inbuilt File class method that is used to open a text file then reads all the text in the file and then closes the file.Syntax:Â Â public static string ReadAllText (string path); Parameter: This function accepts a parameter which is illustrated below:Â Â path: This is th 2 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.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 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 functi 2 min read File.ReadAllBytes() Method in C# with Examples File.ReadAllBytes(String) is an inbuilt File class method that is used to open a specified or created binary file and then reads the contents of the file into a byte array and then closes the file.Syntax:Â Â public static byte[] ReadAllBytes (string path); Parameter: This function accepts a parameter 2 min read File.Replace(String, String, String) Method in C# with Examples File.Replace(String, String, String) is an inbuilt File class method that is used to replace the contents of a specified destination file with the contents of a source file then it deletes the source file and creates a backup of the replaced file.Syntax:Â Â public static void Replace (string sourceFi 3 min read File.Open(String, FileMode) Method in C# with Examples File.Open(String, FileMode) is an inbuilt File class method which is used to open a FileStream on the specified path with read/write access with no sharing.Syntax:Â Â public static System.IO.FileStream Open (string path, System.IO.FileMode mode); Parameter: This function accepts two parameters which 3 min read File.OpenRead() Method in C# with Examples File.OpenRead(String) is an inbuilt File class method which is used to open an existing file for reading.Syntax:Â Â public static System.IO.FileStream OpenRead (string path); Parameter: This function accepts a parameter which is illustrated below:Â Â path: This is the specified file which is going to 2 min read File.OpenText() Method in C# with Examples File.OpenText(String) is an inbuilt File class method which is used to open an existing UTF-8 encoded text file for reading.Syntax:Â Â public static System.IO.StreamReader OpenText (string path); Parameter: This function accepts a parameter which is illustrated below:Â Â path: This is the specified te 2 min read Like