private long GetIndexedDirectoryFilesCount(string path) { using (var reader = new IndexFileReader(IndexPaths.GetIdxFilePath(path))) { return(reader.ReadAll().Count()); } }
private void LookForSequenceInIndexedDirectory(MainViewModel vm, string path, string sequence, ICollection <SearchCondition> conditions = null) { using (var reader = new IndexFileReader(IndexPaths.GetIdxFilePath(path))) { IEnumerable <IndexedItem> items = reader.ReadAll(); //Parallel.ForEach(items, (item) => //{ foreach (var item in items) { var size = (double)item.Length / 1024 / 1024; if (CheckSearchConditions(item, conditions) && CheckDateRange(item, vm.SearchDatesRange) && CheckSizeRange(item, vm.SizeRange)) { if (sequence.Equals("*") || item.Name.ToLower().Contains(sequence.ToLower())) { vm.SearchResults.Add(item); vm.TotalFilesMatch++; _cancellationToken.ThrowIfCancellationRequested(); } vm.TotalFilesSize += size; vm.TotalFilesCount++; vm.CurrentProgressBarValue++; } } //}); } }
private void Index(string path, ProgressDialog dialog) { IList <IndexedItem> items = GetFilesToIndex(new DirectoryInfo(path), dialog); if (dialog.CancellationPending) { File.Delete(IndexPaths.GetIdxFilePath(path)); return; } using (var writer = new IndexFileWriter(IndexPaths.GetIdxFilePath(path))) { writer.Write(items); } }
private void Search(object sender, DoWorkEventArgs e) { var watch = new Stopwatch(); watch.Start(); var vm = e.Argument as MainViewModel; List <SearchCondition> searchConditions = vm.SearchConditions.Select(p => p.Condition).ToList(); var search = Task.Factory.StartNew(() => { foreach (PathItem directory in vm.SearchPaths) { _cancellationToken.ThrowIfCancellationRequested(); if (directory.Checked) { if (!vm.DeepSearch && vm.SearchIndexed && IndexPaths.IdxExists(directory.Path)) { LookForSequenceInIndexedDirectory(vm, directory.Path, vm.SearchSequence, searchConditions); } else { LookForSequenceInDirectory(vm, new DirectoryInfo(directory.Path), vm.SearchSequence, true, searchConditions); } } } }, _cancellationToken); var getSize = Task.Factory.StartNew(() => { long result = 0; foreach (PathItem directory in vm.SearchPaths) { _cancellationToken.ThrowIfCancellationRequested(); if (directory.Checked) { if (!vm.DeepSearch && vm.SearchIndexed && IndexPaths.IdxExists(directory.Path)) { result += GetIndexedDirectoryFilesCount(directory.Path); } else { result += GetFilesCount(new DirectoryInfo(directory.Path), vm, searchConditions); } } } return(result); }, _cancellationToken).ContinueWith(task => vm.ProgressBarMaxValue = task.Result, _cancellationToken); Task.WaitAll(search); // GC forgets to collect the last byte[] read, so we want to collect them _cancellationTokenSource.Cancel(); GC.Collect(); watch.Stop(); vm.CurrentProgressBarValue = 0; vm.ProgressBarMaxValue = 0; var builder = new StringBuilder(); if (Debugger.IsAttached) { builder.AppendFormat("Everything took {0} \n", watch.Elapsed); builder.AppendFormat("Scanned {0} files \n", vm.TotalFilesCount); builder.AppendFormat("Average time for one file is {0}", TimeSpan.FromMilliseconds(((double)watch.ElapsedMilliseconds / vm.TotalFilesCount))); } else { builder.Append("Finished!"); } MessageBox.Show(builder.ToString(), "Finished", MessageBoxButton.OK, MessageBoxImage.Information); }