开发者

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

开发者 https://www.devze.com 2022-12-29 21:43 出处:网络
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 IEnumerabl

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 getF开发者_如何学Goiles(strDir, bCompressedOnly))
            {
                yield return strFile;
            }
        }
    }


I don't think I prefer this but...

return (
        from file in Directory.GetFiles(strDirectory)
        where !bCompressedOnly
            || Path.GetExtension(file).ToLower().Equals(".zip")
        select file
    ).Concat(
        from directory in Directory.GetDirectories(strDirectory)
        from file in getFiles(directory, bCompressedOnly)
        select file
    );

New in .NET 4:

private static IEnumerable<string> getFiles(string strDirectory,
    bool bCompressedOnly)
{
    return Directory.EnumerateFiles(strDirectory,
        bCompressedOnly ? "*.zip" : "*.*", SearchOption.AllDirectories);
}


I take zero credit because I had ReSharper do it for me, but here's what it suggested.

private IEnumerable<string> getFiles(string strDirectory, bool bCompressedOnly) {
    foreach (var strFile in Directory.GetFiles(strDirectory).Where(strFile => !bCompressedOnly || Path.GetExtension(strFile).ToLower().Equals(".zip"))) {
        yield return strFile;
    }

    foreach (var strFile in Directory.GetDirectories(strDirectory).SelectMany(strDir => getFiles(strDir, bCompressedOnly))) {
        yield return strFile;
    }
}

They're all arbitrary changes and in some ways I think (at least in the first loop) they obfuscate the logic a bit, but take it or leave it. Also, check out ReSharper if you haven't already. :)


There is a GetFiles overload method of Directory which takes a search pattern string and a System.IO.SearchOption enumeration. One of SearchOption value is AllDirectories. If it is used, you would get rid of the second loop of your method.

private IEnumerable<string> getFiles(string strDirectory, bool bCompressedOnly)
{
    foreach (var strFile in Directory.GetFiles(strDirectory, bCompressedOnly ? "*.zip" : "*.*", SearchOption.AllDirectories))
    {
        yield return strFile;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消