Ejemplo n.º 1
0
        void SearchFiles(string currentDirectory)
        {
            try
            {
                foreach (string fullFileName in Directory.GetFiles(currentDirectory, FileMask))
                {
                    if (exit)
                    {
                        return;
                    }

                    var file = FileItem.FromFullPath(BaseDirectory, fullFileName);
                    foreach (IFileFinderComparer comparer in comparer)
                    {
                        if (!comparer.FileMatches(file))
                        {
                            file = null;
                            break;
                        }
                    }

                    if (file != null)
                    {
                        FoundFile?.Invoke(this, new FileItemEventArgs(file));
                    }
                }
            }
            catch (Exception ex)
            {
                Error?.Invoke(this, new ErrorEventArgs(ex));
            }
        }
Ejemplo n.º 2
0
        private void RecursiveFileSearch(string searchDir, string filePattern)
        {
            foreach (var f in Directory.GetFiles(searchDir, filePattern))
            {
                mFileList.Add(f);
                FoundFile?.Invoke(f);
            }

            foreach (var d in Directory.GetDirectories(searchDir))
            {
                RecursiveFileSearch(d, filePattern);
            }
        }
        public void SearchFilesInDirectory(string directory, string textToSearch)         //Search files in directory.
        {
            try
            {
                string[] files = Directory.GetFiles(directory);                //Get all files in directory
                foreach (string item in files)
                {
                    string itemToLowerCase = item.ToLower();                          //Make file name in lowercase letters
                    if (itemToLowerCase.Contains(textToSearch.ToLower()))             //Check if file name contains searching text in lowercase letters
                    {
                        FoundFile?.Invoke(this, new ResultEventArgs(SearchId, item)); //If matching file found, invoke event and pass him search ID from DB, and matching file name.
                    }
                }
            }
            catch (UnauthorizedAccessException)
            {
                CatchException?.Invoke(this, new ExceptionEventArgs("There is no required permission to: " + directory, ConsoleColor.Red));
            }
            catch (DirectoryNotFoundException)
            {
                CatchException?.Invoke(this, new ExceptionEventArgs("Path: " + directory + " is not found or is invalid", ConsoleColor.DarkMagenta));
            }
            catch (PathTooLongException)
            {
                CatchException?.Invoke(this, new ExceptionEventArgs("Error:The specified path, file name, or both exceed the system-defined maximum length.", ConsoleColor.DarkRed));
            }
            catch (ArgumentException)
            {
                CatchException?.Invoke(this, new ExceptionEventArgs("Error:path is a zero-length string, contains only white space, or contains one or more invalid characters.", ConsoleColor.DarkCyan));
            }
            catch (IOException)
            {
                CatchException?.Invoke(this, new ExceptionEventArgs("Error. Path: " + directory + " is a file name or a network error has occurred", ConsoleColor.Green));
            }
            catch (Exception ex)
            {
                CatchException?.Invoke(this, new ExceptionEventArgs("Error: " + ex.Message, ConsoleColor.DarkBlue));
            }

            try
            {
                string[] dirs = Directory.GetDirectories(directory);                //Get all directories in selected directory
                foreach (string item in dirs)
                {
                    SearchFilesInDirectory(item, textToSearch);                    //Recursion, search files in every directory in directory.
                }
            }
            catch (UnauthorizedAccessException)
            {
                CatchException?.Invoke(this, new ExceptionEventArgs("There is no required permission to: " + directory, ConsoleColor.Red));
            }
            catch (DirectoryNotFoundException)
            {
                CatchException?.Invoke(this, new ExceptionEventArgs("Path: " + directory + " is not found or is invalid", ConsoleColor.DarkMagenta));
            }
            catch (ArgumentException)
            {
                CatchException?.Invoke(this, new ExceptionEventArgs("Error:path is a zero-length string, contains only white space, or contains one or more invalid characters.", ConsoleColor.DarkCyan));
            }
            catch (PathTooLongException)
            {
                CatchException?.Invoke(this, new ExceptionEventArgs("Error:The specified path, file name, or both exceed the system-defined maximum length.", ConsoleColor.DarkRed));
            }
            catch (IOException)
            {
                CatchException?.Invoke(this, new ExceptionEventArgs("Error. Path: " + directory + " is a file name", ConsoleColor.Green));
            }
            catch (Exception ex)
            {
                CatchException?.Invoke(this, new ExceptionEventArgs("Error: " + ex.Message, ConsoleColor.Blue));
            }
        }