예제 #1
0
    /// <summary>
    /// Ensures that the higher directories iterate first before lower directories
    /// </summary>
    /// <param name="system">Filesystem to be run on</param>
    /// <param name="path">Directory to start enumeration from</param>
    /// <param name="includeSelf">Whether to include the starting directory in the return enumeration</param>
    /// <param name="recursive">Whether to recursively enumerate subdirectories</param>
    /// <returns>Enumerable of contained directories</returns>
    public static IEnumerable <DirectoryPath> EnumerateDirectoryPaths(
        this IDirectory system,
        DirectoryPath path,
        bool includeSelf,
        bool recursive)
    {
        if (!system.Exists(path))
        {
            return(EnumerableExt <DirectoryPath> .Empty);
        }
        var ret = system.EnumerateDirectories(path)
                  .Select(x => new DirectoryPath(x));

        if (recursive)
        {
            ret = ret
                  .Concat(system.EnumerateDirectories(path)
                          .SelectMany(d => system.EnumerateDirectoryPaths(d, includeSelf: false, recursive: true)));
        }
        if (includeSelf)
        {
            ret = path.AsEnumerable().Concat(ret);
        }
        return(ret);
    }