File.ReadLines(String) Method in C# with Examples

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-

file.txt

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

GFG
gfg
Geeks
GeeksforGeeks
geeksforgeeks

Program 2: Initially, a file file.txt is created with some contents shown below-

file.txt

Below code filter some contents from the file and prints them back.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
            }
        }
    }
}

chevron_right


Output:

GFG



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.