I wrote a code to count the number of lines in all the files in a given folder. It works fine, but I am trying to include all the possible C#’s features to refactor it to more compact and efficient code. Please help me do that.
Here is the code.
class LineNumberCounter
{
public static string Calculate(string folderPath, string pattern = "*.txt")
{
DirectoryInfo dirInfo = new DirectoryInfo(folderPath.Trim());
if (!dirInfo.Exists)
throw new ArgumentException("No such directory exists");
StringBuilder returnValue = new StringBuilder();
long totalLines = 0;
pattern.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).All(filter =>
{
int count = 0;
dirInfo.GetFiles(filter.Trim(),
SearchOption.AllDirectories).All(file =>
{
using (StreamReader reader = file.OpenText())
{
for (; reader.Peek() > -1; count++)
reader.ReadLine();
}
returnValue.AppendLine(string.Format("Number of lines with {0} pattern is {1}",
filter, count));
totalLines += count;
return true;
}
);
return true;
});
//foreach (string filter in
// pattern.Split(new char[] { ';' },
// StringSplitOptions.RemoveEmptyEntries))
//{
// FileInfo[] files = dirInfo.GetFiles(filter.Trim(),
// SearchOption.AllDirectories);
// int count = 0;
// Array.ForEach<FileInfo>(files, file =>
// {
// using (StreamReader reader = file.OpenText())
// {
// for (; reader.Peek() > -1; count++)
// reader.ReadLine();
// }
// });
// returnValue.AppendLine(string.Format("Number of lines with {0} pattern is {1}",
// filter, count));
// totalLines += count;
//}
returnValue.AppendLine();
returnValue.AppendLine("Total Lines = " + totalLines);
return returnValue.ToString();
}
}
Commented lines were the ones I wrote originally. I made some attempt to refactor it. But still want to check if it has any more scope.
Using new
>=.NET 4method File.ReadLines()Some considerations from MSDN: