w3resource

C#: Count the number of lines in a file

C# Sharp File Handling: Exercise-15 with Solution

Write a program in C# Sharp to count the number of lines in a file.

Sample Solution:-

C# Sharp Code:

using System; // Importing the System namespace for basic functionalities
using System.IO; // Importing the System.IO namespace for file operations
using System.Text; // Importing the System.Text namespace for text manipulation

class filexercise3 // Declaring a class named filexercise3
{
    public static void Main() // Declaring the Main method
    {
        string fileName = @"mytest.txt"; // Initializing a string variable with the file name
        int count; // Declaring an integer variable to count the lines in the file

        try
        {
            // Delete the file if it exists.
            if (File.Exists(fileName))
            {
                File.Delete(fileName); // Deleting the file if it exists
            }
	
            // Displaying a message to count the number of lines in a file
            Console.Write("\n\n Count the number of lines in a file :\n");
		    Console.Write("------------------------------------------\n");             

            // Create the file and write lines to it.
            using (StreamWriter fileStr = File.CreateText(fileName)) 
            {
                fileStr.WriteLine(" test line 1");
                fileStr.WriteLine(" test line 2");
                fileStr.WriteLine(" Test line 3");
                fileStr.WriteLine(" test line 4");
                fileStr.WriteLine(" test line 5");
                fileStr.WriteLine(" Test line 6");
            }	

            // Read the contents of the file and count the lines
            using (StreamReader sr = File.OpenText(fileName))
            {
                string s = "";
                count = 0; // Initializing the count variable to zero
				Console.WriteLine(" Here is the content of the file mytest.txt : ");
                while ((s = sr.ReadLine()) != null) // Loop through each line in the file
                {
                    Console.WriteLine(s); // Display each line in the console
                    count++; // Incrementing the count for each line
                }
                Console.WriteLine("");
            }

            // Displaying the total number of lines in the file
            Console.Write(" The number of lines in the file {0} is : {1} \n\n", fileName, count);
        }
        catch (Exception MyExcep) // Handling exceptions if any occur
        {
            Console.WriteLine(MyExcep.ToString()); // Displaying exception details
        }
    }
}

Sample Output:

 Count the number of lines in a file :                                                                        
------------------------------------------                                                                    
 Here is the content of the file mytest.txt :                                                                 
 test line 1                                                                                                  
 test line 2                                                                                                  
 Test line 3                                                                                                  
 test line 4                                                                                                  
 test line 5                                                                                                  
 Test line 6                                                                                                  

 The number of lines in  the file mytest.txt is : 6 

Flowchart :

Flowchart: C# Sharp Exercises - Count the number of lines in a file.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous:Write a program in C# Sharp to create and read last n number of lines of a file.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.