// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Provides extension methods to the type. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Web.Extensions { using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; /// /// Provides extension methods to the type. /// public static class DirectoryInfoExtensions { /// /// Returns an enumerable collection of directory information that matches a specified search pattern and search subdirectory option. /// Will return an empty enumerable on exception. Quick and dirty but does what I need just now. /// /// /// The that this method extends. /// /// /// The search string to match against the names of directories. This parameter can contain a combination of valid literal path /// and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files. /// /// /// One of the enumeration values that specifies whether the search operation should include only /// the current directory or all subdirectories. The default value is TopDirectoryOnly. /// /// /// An enumerable collection of directories that matches searchPattern and searchOption. /// public static Task> SafeEnumerateDirectoriesAsync( this DirectoryInfo directoryInfo, string searchPattern = "*", SearchOption searchOption = SearchOption.TopDirectoryOnly) { return Task.Run(() => SafeEnumerateDirectories(directoryInfo, searchPattern, searchOption)); } /// /// Returns an enumerable collection of directory information that matches a specified search pattern and search subdirectory option. /// Will return an empty enumerable on exception. Quick and dirty but does what I need just now. /// /// /// The that this method extends. /// /// /// The search string to match against the names of directories. This parameter can contain a combination of valid literal path /// and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files. /// /// /// One of the enumeration values that specifies whether the search operation should include only /// the current directory or all subdirectories. The default value is TopDirectoryOnly. /// /// /// An enumerable collection of directories that matches searchPattern and searchOption. /// public static IEnumerable SafeEnumerateDirectories( this DirectoryInfo directoryInfo, string searchPattern = "*", SearchOption searchOption = SearchOption.TopDirectoryOnly) { IEnumerable directories; try { directories = directoryInfo.EnumerateDirectories(searchPattern, searchOption); } catch { return Enumerable.Empty(); } return directories; } } }