Ejemplo n.º 1
0
 private void FileFound(Uri uri)
 {
     TraceMessage($"Found file to analyze {uri}");
     OnFileFound?.Invoke(this, new FileFoundEventArgs {
         uri = uri
     });
 }
Ejemplo n.º 2
0
 public void RunSearch()
 {
     try
     {
         foreach (var filename in Directory.EnumerateFiles
                      (SearchRequest.SearchRootDirectory, SearchRequest.SearchString, SearchOption.AllDirectories))
         {
             DbManager.RecordSearchResult(SearchRequest.SearchRequestId, filename);
             OnFileFound?.Invoke(SearchRequest, filename);
             NumberOfResults++;
         }
         SearchRequest.IsComplete = true;
         DbManager.SetSearchRequestStatus(SearchRequest);
     }
     catch (DirectoryNotFoundException ex)
     {
         Console.WriteLine($"Directory not found: {ex.Message}.");
     }
     catch (UnauthorizedAccessException ex)
     {
         Console.WriteLine($"Exception running the search: {ex.Message}");
     }
     catch (Exception ex)
     {
         Console.WriteLine($"Search failed with the following exception: {ex.Message}.");
     }
 }
Ejemplo n.º 3
0
        public void ShouldCancel()
        {
            OnFileFound += EventBehaviour_OnFileFound;

            FileFoundEventArgs args = new FileFoundEventArgs(@"c:\temp\1.txt");

            OnFileFound.Invoke(this, args);
            if (args.Cancel)
            {
                return;
            }
        }
Ejemplo n.º 4
0
        //public delegate void DoSomething(int a); ==Action<int>
        //public delegate int DoSomething(int a); ==Func<int, int>

        //FileSystemVisitor(FileSystemBlaBla fs)
        //{
        //    this.fs = fs;
        //}

        public List <string> GetFiles(Func <FileSystemInfo, bool> condition, FileSystemInfo dirFiles)
        {
            var result = new List <string>();

            //logic for looping through files
            try
            {
                OnSearchStart?.Invoke("Search has started");
                foreach (string d in Directory.GetDirectories(dirFiles.ToString()))
                {
                    if (Stop)
                    {
                        return(result);
                    }
                    var dir = new DirectoryInfo(d);
                    if (condition(dir))
                    {
                        if (!Exclude)
                        {
                            result.Add(d);
                        }
                        Exclude = false;
                        OnDirectoryFound?.Invoke(dir);
                    }
                    var r = GetFiles(d => condition(d), dir);
                    result.AddRange(r);
                }
                foreach (string f in Directory.GetFiles(dirFiles.ToString()))
                {
                    if (Stop)
                    {
                        return(result);
                    }
                    var fi = new FileInfo(f);
                    if (condition(fi))
                    {
                        if (!Exclude)
                        {
                            result.Add(f);
                        }
                        OnFileFound?.Invoke(fi);
                    }
                }
            }
            catch (System.Exception excpt)
            {
                Console.WriteLine(excpt.Message);
            }
            OnSearchFinish?.Invoke("Search has finished");
            return(result);
        }
        public async IAsyncEnumerable <IFileItem> Search(string path, string pattern, [EnumeratorCancellation] CancellationToken cancellationToken, PauseToken pauseToken)
        {
            pattern ??= "";

            DirectoryInfo dirInfo = new DirectoryInfo(path);

            foreach (DirectoryInfo directory in dirInfo.GetDirectories())
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    yield break;
                }

                if (pauseToken.IsPaused)
                {
                    await pauseToken.WaitWhilePausedAsync();
                }

                IEnumerable <IFileItem> searchedItems = await Search(directory.FullName, pattern, cancellationToken, pauseToken).ToListAsync();

                if (searchedItems.Count() > 0)
                {
                    DirectoryItem item = new DirectoryItem
                    {
                        Name  = directory.Name,
                        Path  = directory.FullName,
                        Items = searchedItems
                    };

                    // Имитация задержки поиска файлов
                    await Task.Delay(1);

                    yield return(item);
                }
            }

            foreach (FileInfo file in dirInfo.GetFiles().Where(n => Regex.IsMatch(n.Name, pattern)))
            {
                FileItem item = new FileItem
                {
                    Name = file.Name,
                    Path = file.FullName
                };

                OnFileFound?.Invoke(1);

                yield return(item);
            }
        }