ImageGlass.Library.DirectoryFinder.FindFiles C# (CSharp) Method

FindFiles() public static method

Returns a list of files under RootDirectory
public static FindFiles ( string RootDirectory, bool SearchAllDirectories, Predicate Filter ) : List
RootDirectory string >starting directory
SearchAllDirectories bool >when true, all sub directories will be searched as well
Filter Predicate filter to be done on files/directory. use null for no filtering
return List
        public static List<string> FindFiles(string RootDirectory,
            bool SearchAllDirectories, Predicate<string> Filter)
        {
            List<string> retList = new List<string>();

            try
            {
                // get the list of directories
                List<string> DirList = new List<string> { RootDirectory };

                // get sub directories if allowed
                if (SearchAllDirectories)
                    DirList.AddRange(FindDirectories(RootDirectory, true, null));

                // loop through directories populating the list
                Parallel.ForEach(DirList, folder =>
                {
                    // get a directory object
                    DirectoryInfo di = new DirectoryInfo(folder);

                    try
                    {
                        // loop through the files in this directory
                        foreach (FileInfo file in di.GetFiles())
                        {
                            try
                            {
                                // add the file if it passes the filter
                                if ((Filter == null) || (Filter(file.FullName)))
                                    retList.Add(file.FullName);
                            }

                            catch (Exception ex)
                            {
                                // TODO: log the exception
                            }
                        }
                    }

                    catch (UnauthorizedAccessException)
                    {
                        // don't really need to do anything
                        // user just doesn't have access
                    }

                    catch (Exception ex)
                    {
                        // TODO: log the exception
                    }
                });
            }

            catch (Exception ex)
            {
                // TODO: save exception
            }

            // return the list
            return retList;
        }