/// <summary>
    /// fluent setter for <see cref="ISupportSwitchRecurseSubdirectories"/>.
    /// </summary>
    /// <typeparam name="T">the builder to support the <see cref="ISupportSwitchRecurseSubdirectories"/>.</typeparam>
    /// <param name="this">The builder-instance.</param>
    /// <param name="type">The Recurse-type.</param>
    /// <returns>The builder-instance for fluent re-use.</returns>
    public static T WithRecurseSubdirectories <T>(this T @this, RecurseType type)
        where T : ISupportSwitchBuilder <ISupportSwitchRecurseSubdirectories>
    {
        @this.Command.RecurseSubdirectories = new SwitchRecurseSubdirectories(type);

        return(@this);
    }
    /// <summary>
    /// fluent setter for <see cref="ISupportSwitchExcludeFilenames"/>.
    /// </summary>
    /// <typeparam name="T">the builder to support the <see cref="ISupportSwitchExcludeFilenames"/>.</typeparam>
    /// <param name="this">The builder-instance.</param>
    /// <param name="type">The <see cref="RecurseType"/>.</param>
    /// <param name="fileNameWildcard">The filename-wildcard.</param>
    /// <param name="additional">Additional filename-wildcards.</param>
    /// <returns>The builder-instance for fluent re-use.</returns>
    public static T WithExcludeFilenames <T>(this T @this, RecurseType type, string fileNameWildcard, params string[] additional)
        where T : ISupportSwitchBuilder <ISupportSwitchExcludeFilenames>
    {
        var first = new SwitchExcludeFilename(fileNameWildcard, type);
        var rest  = additional.Select(x => new SwitchExcludeFilename(x, type));

        return(@this.WithExcludeFilenames(first, rest));
    }
Ejemplo n.º 3
0
        public static System.Collections.Generic.IEnumerable <string> RecurseFilesInDirectories(string root, RecurseType recurseType = RecurseType.Files)
        {
            // Data structure to hold names of sub-folders to be
            // examined for files.
            Stack <string> dirs = new Stack <string>(20);

            if (!System.IO.Directory.Exists(root))
            {
                yield break;
                // throw new ArgumentException();
            }
            dirs.Push(root);

            while (dirs.Count > 0)
            {
                var currentDir = dirs.Pop();
                if (recurseType == RecurseType.Directories || recurseType == RecurseType.FilesDirectories)
                {
                    yield return(currentDir);
                }

                string[] subDirs;
                try
                {
                    subDirs = System.IO.Directory.GetDirectories(currentDir);
                }
                // An UnauthorizedAccessException exception will be thrown if we do not have
                // discovery permission on a folder or file.
                // to ignore the exception and continue enumerating the remaining files and
                // folders. It is also possible (but unlikely) that a DirectoryNotFound exception
                // will be raised. This will happen if currentDir has been deleted by
                // another application or thread after our call to Directory.Exists. The
                catch (UnauthorizedAccessException)
                {
                    continue;
                }
                catch (System.IO.DirectoryNotFoundException)
                {
                    continue;
                }

                string[] files = null;
                try
                {
                    files = System.IO.Directory.GetFiles(currentDir);
                }

                catch (UnauthorizedAccessException)
                {
                    //Console.WriteLine(e.Message);
                    continue;
                }

                catch (System.IO.DirectoryNotFoundException)
                {
                    //Console.WriteLine(e.Message);
                    continue;
                }

                if (recurseType == RecurseType.Files || recurseType == RecurseType.FilesDirectories)
                {
                    foreach (var file in files)
                    {
                        yield return(file);
                    }
                }

                // Push the subdirectories onto the stack for traversal.
                // This could also be done before handing the files.
                foreach (var subdir in subDirs)
                {
                    dirs.Push(subdir);
                }
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SwitchRecurseSubdirectories"/> class.
 /// </summary>
 /// <param name="recurse">The recuse-type.</param>
 public SwitchRecurseSubdirectories(RecurseType recurse)
 {
     this.recurse = recurse;
 }