You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue is a proposal to add Count() to FileInfo to provide the ability to count the total number of lines in a file (no filter specified) or count the specific number of occurrences of a string in a file (using the filter).
/// <summary>
/// Gets the total number of lines in a file.
/// Note this method is optimized for large files and must count each line so it may be expensive for large files.
/// </summary>
/// <param name="this">The file to perform the count on.</param>
/// <param name="filter">If specified, finds the number of concurrences of the string in the file.</param>
/// <returns>
/// A count of the number of lines in the file.
/// </returns>
public static long Count(this FileInfo @this, string filter = null)
{
using (var reader = @this.OpenText())
{
var count = 0L;
if (filter == null)
{
while (!reader.EndOfStream)
{
reader.ReadLine();
count++;
}
}
else
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if(line != null && line.Contains(filter))
count++;
}
}
return count;
}
}
The text was updated successfully, but these errors were encountered:
This issue is a proposal to add
Count()
to FileInfo to provide the ability to count the total number of lines in a file (no filter specified) or count the specific number of occurrences of a string in a file (using the filter).The text was updated successfully, but these errors were encountered: