Is there a way to make this C# method shorter and more readable with the help of Linq?
- by Hamish Grubijan
The following works, but I figured - since it is all based on IEnumerable, Linq can come handy here is well. By the way, is there an equivalent to Directory.GetFiles() which would return an IEnumerable instead of the array? If it exists, then would it make the code run any faster? The last part of the question is inspired by Python language which favors lightweight generators over concrete lists.
private IEnumerable<string> getFiles(string strDirectory, bool bCompressedOnly)
{
foreach (var strFile in Directory.GetFiles(strDirectory))
{
// Don't add any existing Zip files since we don't want to delete previously compressed files.
if (!bCompressedOnly || Path.GetExtension(strFile).ToLower().Equals(".zip"))
{
yield return strFile;
}
}
foreach (var strDir in Directory.GetDirectories(strDirectory))
{
foreach (var strFile in getFiles(strDir, bCompressedOnly))
{
yield return strFile;
}
}
}