Ejemplo n.º 1
0
        /// <summary>
        /// Searches the given folder for all children that match the given filter
        /// Folders are placed before files in the output
        /// </summary>
        /// <param name="folderPath">A path to a folder(NOT a file)</param>
        /// <param name="filter">The filter that all returned files must match</param>
        /// <param name="sortMethod">The method used to sort the output</param>
        /// <param name="descending"></param>
        /// <returns>An enumerable of TaggedFilePaths that match the given filter</returns>
        public static IEnumerable <TaggedFilePath> GetMatchingFiles(
            string folderPath,
            TagFilter filter,
            SortMethod sortMethod = SortMethod.name,
            bool descending       = false)
        {
            // Decide which funciton will be used to sort the files in the list
            SortFunction sortFunction = sortMethod.GetSortFunction();

            // Get all files/folders that match the filter
            DirectoryInfo folder = new DirectoryInfo(folderPath);

            IEnumerable <TaggedFilePath> files =
                from FileInfo fsInfo in folder.EnumerateFiles()
                let path = new TaggedFilePath(fsInfo.FullName, false)
                           where filter.Matches(path)
                           orderby sortFunction(path) ascending
                           select path;

            IEnumerable <TaggedFilePath> folders =
                from DirectoryInfo fsInfo in folder.EnumerateDirectories()
                let path = new TaggedFilePath(fsInfo.FullName, true)
                           where filter.Matches(path)
                           orderby sortFunction(path) ascending
                           select path;

            // Sort them by descending, if the box is checked
            if (descending)
            {
                files   = files.Reverse();
                folders = folders.Reverse();
            }

            // Combine the folders and files into the same list
            // Folders are added first for easy navigation
            return(folders.Concat(files));
        }