Is there a way to make this C# method shorter and more readable with the help of Linq?

Posted by Hamish Grubijan on Stack Overflow See other posts from Stack Overflow or by Hamish Grubijan
Published on 2010-05-12T15:29:07Z Indexed on 2010/05/12 21:24 UTC
Read the original article Hit count: 177

Filed under:
|
|

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;
            }
        }
    }

© Stack Overflow or respective owner

Related posts about .net-3.5

Related posts about LINQ