.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> File Operation >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Counting Number of Lines Using Stream

Posted By:Ravi Ranjan Kumar       Posted Date: June 29, 2011    Points: 40    Category: File Operation    URL: http://www.dotnetspark.com  

This Code Snippets enable you to learn how to Count Total Numbers of Lines using Stream in C#
 

Import IO namespace to access it class in program

using System.IO;
after that you can use given function to get total number of lines 

public static int CountLine(string fname)
        {
            int count = 0;
            using (StreamReader sr = File.OpenText(fname))
            {
                while (sr.ReadLine() != null)
                {
                    count++;
                }
            }
            return count;
        }
this function will take filename as a parameter and return total number of line of file as output 

How to use this function

 its very simple to use this function! take a look how to use 

int i = CountLine(@"YourFileNamepath");

Countline function gives total number to i so after that i will responsible to hold total number of file. 


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend