Home
Manage Your Code
Snippet: File Count Lines (C#)
Title: File Count Lines Language: C#
Description: Very fast method to count the lines in a file. Views: 623
Author: Adam Emrick Date Added: 9/18/2007
Copy Code  
1private int CountFileLines(string FileName)
2        {
3            TextReader reader = File.OpenText(FileName);
4            char[] buffer = new char[32 * 1024]; // Read 32K chars at a time
5            int total = 1; // All files have at least one line!
6            int read;
7            while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
8            {
9                for (int i = 0; i < read; i++)
10                {
11                    if (buffer[i] == '\n')
12                    {
13                        total++;
14                    }
15                }
16            }
17            return total;
18        }