SecureDelete.FileSearch.FileSearcher.SearchFilesImpl C# (CSharp) Method

SearchFilesImpl() private method

private SearchFilesImpl ( string folder, string pattern, bool includeSubfolders, bool async ) : string[]
folder string
pattern string
includeSubfolders bool
async bool
return string[]
        private string[] SearchFilesImpl(string folder, string pattern, bool includeSubfolders, bool async)
        {
            // check the parameters
            Debug.AssertNotNull(folder, "Folder is null");
            List<string> files = null;

            if(GetStop() == true) {
                return null;
            }

            try {
                // get the files
                string[] tempFiles = null;

                if(pattern == null || pattern.Length == 0) {
                    tempFiles = Directory.GetFiles(folder);
                }
                else {
                    if(regex == null) {
                        tempFiles = Directory.GetFiles(folder, pattern);
                    }
                    else {
                        // apply regex match
                        tempFiles = Directory.GetFiles(folder, "*.*"); // get all files in this case
                        List<string> matchingFiles = new List<string>();
                        int count = tempFiles.Length;

                        for(int i = 0; i < count; i++) {
                            FileInfo info = new FileInfo(tempFiles[i]);

                            if(regex.IsMatch(info.Name)) {
                                matchingFiles.Add(tempFiles[i]);
                            }

                            if(i % 32 == 0 && GetStop() == true) {
                                return null;
                            }
                        }

                        tempFiles = matchingFiles.ToArray();
                    }
                }

                // notify the user about the new files
                if(tempFiles.Length > 0) {
                    if(async) {
                        if(_fileFilter != null) {
                            // SPEED TESTING - START
                            //Debug.PerformanceManager.AddEvent("photo_filter", true);

                            List<string> validFiles = new List<string>(_resultChunkLength);
                            int count = tempFiles.Length;

                            for(int i = 0; i < count; i++) {
                                if(_fileFilter.AllowFile(tempFiles[i])) {
                                    validFiles.Add(tempFiles[i]);
                                }

                                if((i > 0 && (i % _resultChunkLength) == 0) ||
                                    MaximumWaitTimeExceeded() == true) {
                                    SendFiles(validFiles.ToArray(), true);
                                    validFiles.Clear();
                                }
                            }

                            // SPEED TESTING - END
                            //Debug.PerformanceManager.StopEvent("photo_filter");
                            //Debug.PerformanceManager.GenerateHtmlSummary();

                            // send last chunk
                            if(validFiles.Count > 0) {
                                SendFiles(validFiles.ToArray(), false);
                            }

                            // stop execution
                            if(GetStop() == true) {
                                return null;
                            }
                        }
                        else {
                            SendFiles(tempFiles, false);
                        }

                        // stop execution
                        if(GetStop() == true) {
                            return null;
                        }
                    }
                    else {
                        // process the files
                        if(_fileFilter != null) {
                            tempFiles = ProcessFiles(tempFiles);
                        }

                        // append the files to the list
                        files = new List<string>();
                        files.AddRange(tempFiles);
                    }
                }

                // ****************************************************************************************************
                // Subfolders
                // ****************************************************************************************************
                if(includeSubfolders) {
                    string[] tempDirs = Directory.GetDirectories(folder);

                    if(tempDirs != null && tempDirs.Length > 0) {
                        for(int i = 0; i < tempDirs.Length; i++) {
                            tempFiles = SearchFilesImpl(tempDirs[i], pattern, includeSubfolders, async);

                            if(tempFiles != null && tempFiles.Length > 0 && async == false) {
                                if(files == null) {
                                    files = new List<string>();
                                }

                                files.AddRange(tempFiles);
                            }

                            if(GetStop() == true) {
                                return null;
                            }
                        }
                    }
                }
            }
            catch(Exception e) {
                Debug.ReportError("Error while searching files. Exception: {0}", e.Message);
            }

            if(files != null) {
                return files.ToArray();
            }
            else {
                return null;
            }
        }