File Count Lines

Settembre 19th, 2007

Description: Very fast method to count the lines in a file.

Link: http://www.codekeep.net/snippets/84502287-a10f-4f15-871b-86df47f9d8f4.aspx

private int CountFileLines(string FileName)
        {
            TextReader reader = File.OpenText(FileName);
            char[] buffer = new char[32 * 1024]; // Read 32K chars at a time
            int total = 1; // All files have at least one line!
            int read;
            while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
            {
                for (int i = 0; i < read; i++)
                {
                    if (buffer[i] == '\n')
                    {
                        total++;
                    }
                }
            }
            return total;
        }

  • .NET
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Trackback URI | Comments RSS

Leave a Reply

You must be logged in to post a comment.