w3resource logo


C# Sharp Programming Exercises

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

Secondary Nav

C# Sharp File Handling : Exercise-15 with Solution

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

 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         

C# Sharp Code:

using System;
using System.IO;
using System.Text;

class filexercise3
{
    public static void Main()
    {
        string fileName = @"mytest.txt"; 
         int count;

        try
        {
            // Delete the file if it exists.
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
	        Console.Write("\n\n Count the number of lines in a file :\n");
		    Console.Write("------------------------------------------\n");             
            // Create the file.
            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");
            }	
            using (StreamReader sr = File.OpenText(fileName))
            {
                string s = "";
                count=0;
				Console.WriteLine(" Here is the content of the file mytest.txt : ");
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                    count++;
                }
                Console.WriteLine("");
            }
            Console.Write(" The number of lines in  the file {0} is : {1} \n\n",fileName,count);
        }
        catch (Exception MyExcep)
        {
            Console.WriteLine(MyExcep.ToString());
        }
    }
}

C# Sharp Practice online:


using System;
using System.IO;
using System.Text;

class filexercise3
{
    public static void Main()
    {
        string fileName = @"mytest.txt"; 
         int count;

        try
        {
            // Delete the file if it exists.
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
	        Console.Write("\n\n Count the number of lines in a file :\n");
		    Console.Write("------------------------------------------\n");             
            // Create the file.
            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");
            }	
            using (StreamReader sr = File.OpenText(fileName))
            {
                string s = "";
                count=0;
				Console.WriteLine(" Here is the content of the file mytest.txt : ");
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                    count++;
                }
                Console.WriteLine("");
            }
            Console.Write(" The number of lines in  the file {0} is : {1} \n\n",fileName,count);
        }
        catch (Exception MyExcep)
        {
            Console.WriteLine(MyExcep.ToString());
        }
    }
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program