public void Search(string directory, string searchPattern) { foreach (var file in Directory.EnumerateFiles(directory, searchPattern)) { FileFound?.Invoke(this, new FileFoundArgs(file)); } }
public bool FindFileByName(string name, string currentPath) { var files = GetFiles(currentPath); if (files != null) { foreach (var file in files) { if (file.Name.ToLower().Contains(name)) { var folderContent = GetFolderContent(currentPath).ToList(); FileFound?.Invoke(folderContent.FindIndex(s => s.FullName.ToLower().Contains(name.ToLower())), currentPath, folderContent.Count); return(true); } } } var folders = GetFolders(currentPath); if (folders != null) { foreach (var folder in folders) { var result = FindFileByName(name, folder.FullName); if (result) { return(true); } } } return(false); }
private void _watcher_Created(FileSystemEventArgs e) { if (e.Name.ToUpper().StartsWith("QUERY_DATA")) { _log.Info("File found via file '" + e.ChangeType + "; importing."); Application.Current.Dispatcher.BeginInvoke(new Action(() => { FileFound?.Invoke(e.FullPath); })); } }
void OnFileFound(string filename) { Application.Current.Dispatcher.Invoke ( () => FileFound?.Invoke(this, new ExplorerEventArgs { FileName = filename }) ); }
private void SearchFiles(string path, string searchValue, int searchId) { searchValue = searchValue.ToLower(); // Search Files #region SearchFiles string[] files = Directory.GetFiles(path); foreach (string file in files) { try { // Get file Info #region GetFileInfo string fileName = Path.GetFileNameWithoutExtension(file); string filePath = Path.GetFullPath(file); string fileExtension = Path.GetExtension(file); #endregion FileCounter++; if (fileName.ToLower().Contains(searchValue)) { // Add Result to DB and Notify File Found using (ResultLogic logic = new ResultLogic()) { FileFoundCounter++; logic.AddResult(fileName, filePath, searchId); FileFound?.Invoke(this, new FileEventArgs(fileName, fileExtension, filePath)); } } } #region Exceptions catch (PathTooLongException ex) { UnreachableCounter++; TotalErrorCounter++; } catch (Exception ex) { UnhandledErrors++; TotalErrorCounter++; ErrorLog.Add(ex.Message); } #endregion } #endregion // Search Directories #region SearchDirectories string[] directories = Directory.GetDirectories(path); foreach (string d in directories) { try { SearchFiles(d, searchValue, searchId); } }
protected virtual void OnFileFound(ContentFindedEventArgs e) { if (ReferenceEquals(e, null)) { throw new ArgumentNullException(); } FileFound?.Invoke(this, e); _searchStopped = e.StopSearch; }
IEnumerable <FileSystemInfo> GetFileSystemInfo(DirectoryInfo directoryInfo) { var allElements = directoryInfo.GetFileSystemInfos(); foreach (var fileSystemInfo in allElements) { var itemFoundEventArgs = new FileSystemInfoEventArgs(fileSystemInfo.FullName); var itemFilteredEventArgs = new FilteredFileSystemInfoEventArgs(fileSystemInfo.FullName); if (fileSystemInfo is FileInfo) { FileFound?.Invoke(this, itemFoundEventArgs); if (itemFoundEventArgs.StopSearch) { yield break; } if (_matchPredicate(fileSystemInfo) && !itemFoundEventArgs.Exclude) { FileFiltered?.Invoke(this, itemFilteredEventArgs); if (itemFilteredEventArgs.StopSearch) { yield break; } if (!itemFilteredEventArgs.Exclude) { yield return(fileSystemInfo); } } } else if (fileSystemInfo is DirectoryInfo nextDirectory) { DirectoryFound?.Invoke(this, itemFoundEventArgs); if (itemFoundEventArgs.StopSearch) { yield break; } if (_matchPredicate(fileSystemInfo) && !itemFoundEventArgs.Exclude) { foreach (var nextFileSystemInfo in GetFileSystemInfo(nextDirectory)) { yield return(nextFileSystemInfo); } DirectoryFiltered?.Invoke(this, itemFilteredEventArgs); if (itemFilteredEventArgs.StopSearch) { yield break; } if (!itemFilteredEventArgs.Exclude) { yield return(fileSystemInfo); } } } } }
private void SearchDirectory(string directory, string searchPattern) { foreach (var file in Directory.EnumerateFiles(directory, searchPattern)) { var args = new FileFoundArgs(file); FileFound?.Invoke(this, args); if (args.CancelRequested) { break; } } }
internal void SearchFilesInDirectory(string directory, string searchPattern) { foreach (var file in Directory.EnumerateFiles(directory, searchPattern)) { PathDecomposition pathDecomp = new PathDecomposition(file, CurrentDirectory); pathDecomp.PuttingItTogether(); var args = new FileFoundArgs(file); FileFound?.Invoke(this, args); //if (args.CancelRequested) // break; } }
private void ProcessFoundEntry(FileSystemInfoBase systemInfoBase) { var isDirectory = systemInfoBase.Attributes.HasFlag(FileAttributes.Directory); if (isDirectory) { DirectoryFound?.Invoke(systemInfoBase, EventArgs.Empty); } else { FileFound?.Invoke(systemInfoBase, EventArgs.Empty); } }
private void FileSearch(string path) { foreach (string file in Directory.GetFiles(path)) { if (!_subscription) { return; } FileFound?.Invoke(this, new FileArgs(file)); Thread.Sleep(1000); } CatalogSearch(path); }
private void ScanFile(string file) { try { if (CheckForValidHeader && !DicomFile.HasValidHeader(file)) { return; } var df = DicomFile.Open(file); FileFound?.Invoke(this, df, file); } catch { // ignore exceptions? } }
private void GetFiles(string path, string pattern, string searchKey) //searches the directories for targeted files { var File = new List <string>(); try { if (searchTrigger) { Console.WriteLine("search stopped, press any key to return."); if (Console.KeyAvailable) { Console.ReadKey(); } Console.ReadKey(); Console.Clear(); SearchHandler handler = new SearchHandler(); handler.Start(); } //get all files directly within a certain directory and checking them for out search input // displaying matches in real time, and adding them to the list for transfer to database. File.AddRange(Directory.GetFiles(path, pattern, SearchOption.TopDirectoryOnly)); foreach (var item in File) { if (Path.GetFileNameWithoutExtension(item).ToLower().Contains(searchKey.ToLower())) { Counter++; FileFound?.Invoke(this, new SearchResultEvent(item, Counter)); } } // //performing the same action on every directoy in our path foreach (var directory in Directory.GetDirectories(path)) { GetFiles(directory, pattern, searchKey); } // } catch (UnauthorizedAccessException) { } catch (PathTooLongException) { } }
private bool VisitFile(FileInfo fileInfo, FolderNode rootNode) { if (_searchIsStopped) { return(false); } var file = Map(fileInfo, rootNode); var filterResult = _filterBy == null ? true : _filterBy.Invoke(file); if (filterResult) { var fEvent = new FileNodeFindEvent(file); FileFound?.Invoke(this, fEvent); ProcessEvent(rootNode, file, fEvent); return(fEvent.ShouldBeAdd); } return(false); }
private void TestFile(FileInfo file, FileQueryFilterList filters) { if (Logger.IsDebugEnabled) { Logger.Debug("Testing file: " + file); } bool accept = true; foreach (var filter in filters) { if (abortSearch || accept == false) { // No need to continue break; } // Apply the filter if (Logger.IsDebugEnabled) { Logger.Debug("Checking filter: " + filter.ToString()); } accept = filter.Accept(file); } if (accept) { if (Logger.IsDebugEnabled) { Logger.Debug("File passed all filters: " + file); } Results.Add(file); FileFound?.Invoke(this, new FileFoundEventArgs() { fileInfo = file }); } }
protected virtual void OnFileFound(HyperFileCheckEventArgs e) { FileFound?.Invoke(this, e); }
protected void OnFileFoundReached(FoundEventArgs e) { FileFound?.Invoke(this, e); }
private void SearchFiles(string path, string filters, bool searchRecursively, bool searchHidden, FoundFilesOrderByFilter order) { try { LOGGER.Info($"Searching for files"); LOGGER.Info($"Parameters => path: '{path}', filters: '{filters}', recursive: {searchRecursively}, hidden: {searchHidden}, order by: {order}"); runningCount++; State = RunningState.Running; if (State == RunningState.CancelPending) { LOGGER.Info($"Canceling search"); return; } if (!searchHidden && new DirectoryInfo(path).Attributes.HasFlag(FileAttributes.Hidden)) { LOGGER.Info($"Path '{path}' is hidden and hidden search is disabled => skipping directory"); return; } string[] singleFilters = filters.Split(';'); foreach (var filter in singleFilters) { var files = Directory.GetFiles(path, filter).Select(p => new FileInfo(p)).ToArray(); LOGGER.Info($"Found {files.Length} files for filter '{filter}' in path '{path}'"); switch (order) { case FoundFilesOrderByFilter.NameAsc: files = files.OrderBy((file) => file.Name).ToArray(); break; case FoundFilesOrderByFilter.NameDsc: files = files.OrderByDescending((file) => file.Name).ToArray(); break; case FoundFilesOrderByFilter.CreationDateAsc: files = files.OrderBy((file) => file.CreationTimeUtc).ToArray(); break; case FoundFilesOrderByFilter.CreationDateDsc: files = files.OrderByDescending((file) => file.CreationTimeUtc).ToArray(); break; case FoundFilesOrderByFilter.ChangedDateAsc: files = files.OrderBy((file) => file.LastWriteTimeUtc).ToArray(); break; case FoundFilesOrderByFilter.ChangedDateDsc: files = files.OrderByDescending((file) => file.LastWriteTimeUtc).ToArray(); break; case FoundFilesOrderByFilter.SizeAsc: files = files.OrderBy((file) => file.Length).ToArray(); break; case FoundFilesOrderByFilter.SizeDsc: files = files.OrderByDescending((file) => file.Length).ToArray(); break; default: files = files.OrderBy((file) => file.Name).ToArray(); break; } foreach (var file in files.Where(f => !f.Name.StartsWith("_") && IsVideoAnalyzer.IsVideo(f.Name))) { LOGGER.Info($"Found video file '{file.FullName}'"); FileFound?.Invoke(new FileSystemEventArgs(WatcherChangeTypes.All, file.DirectoryName, file.Name)); } } if (searchRecursively) { LOGGER.Info($"Recursive mode is enabled => searching sub directories"); Directory.GetDirectories(path) .ToList() .ForEach(s => SearchFiles(s, filters, true, searchHidden, order)); } } catch (UnauthorizedAccessException ex) { LOGGER.Error($"Access to directory was denied", ex); } catch (ThreadAbortException ex) { LOGGER.Error($"Search thread was aborted", ex); } finally { LOGGER.Info($"Search for files in directory '{path}' has been finished"); runningCount--; if (runningCount == 0) { LOGGER.Info($"Searcher finished search"); State = RunningState.NotRunning; } } }
public void TraverseTree(string root) { // Data structure to hold names of subfolders to be // examined for files. Stack <string> dirs = new Stack <string>(20); if (!System.IO.Directory.Exists(root)) { throw new ArgumentException(); } dirs.Push(root); while (dirs.Count > 0 && HasCancelFlag == false) { string currentDir = dirs.Pop(); string[] subDirs; try { subDirs = System.IO.Directory.GetDirectories(currentDir); } catch (UnauthorizedAccessException e) { Console.WriteLine(e.Message); continue; } catch (System.IO.DirectoryNotFoundException e) { Console.WriteLine(e.Message); continue; } string[] files = null; try { files = System.IO.Directory.GetFiles(currentDir); } catch (UnauthorizedAccessException e) { Console.WriteLine(e.Message); continue; } catch (System.IO.DirectoryNotFoundException e) { Console.WriteLine(e.Message); continue; } // Perform the required action on each file here. // Modify this block to perform your required task. foreach (string file in files) { try { // Perform whatever action is required in your scenario. System.IO.FileInfo fi = new System.IO.FileInfo(file); //задание №2 action?.Invoke(fi); //задание №3 FileFound?.Invoke(this, new FileArgs { FileName = fi.Name }); if (HasCancelFlag) { break; } } catch (System.IO.FileNotFoundException e) { // If file was deleted by a separate application // or thread since the call to TraverseTree() // then just continue. Console.WriteLine(e.Message); continue; } } // Push the subdirectories onto the stack for traversal. // This could also be done before handing the files. foreach (string str in subDirs) { dirs.Push(str); } } }
protected virtual void OnFileFound(FileEventArgs args) { FileFound?.Invoke(this, args); }
private FileSystemInfoCustomCollection <CustomFileItem> GetItemsRecursively(string directoryPath, FilterMask filterMask) { var directoryInfo = (CustomDirectoryInfo)customDirectoryInfoFactory.CreateInstance(directoryPath); var output = new FileSystemInfoCustomCollection <CustomFileItem>(); if (isFirstFilteredFileFound && filterMask.HasFlag(FilterMask.FirstOnly)) { return(output); } if (!filterMask.HasFlag(FilterMask.NoFolders)) { DirectoryFound?.Invoke(this, new ItemFoundArgs { Item = directoryInfo }); if (filterMask.HasFlag(FilterMask.IgnoreFilterFunction) || FileFilterDelegate.Invoke(directoryInfo)) { FilteredDirectoryFound?.Invoke(this, new ItemFoundArgs { Item = directoryInfo }); output.Add(directoryInfo); isFirstFilteredFileFound = true; if (isFirstFilteredFileFound && filterMask.HasFlag(FilterMask.FirstOnly)) { return(output); } } } var files = directoryInfo.GetFiles(); foreach (var file in files) { FileFound?.Invoke(this, new ItemFoundArgs { Item = file }); if (filterMask.HasFlag(FilterMask.IgnoreFilterFunction) || FileFilterDelegate.Invoke(file)) { FilteredFileFound?.Invoke(this, new ItemFoundArgs { Item = file }); output.Add(file); isFirstFilteredFileFound = true; if (isFirstFilteredFileFound && filterMask.HasFlag(FilterMask.FirstOnly)) { return(output); } } } var directories = directoryInfo.GetDirectories(); foreach (var directory in directories) { var result = GetItemsRecursively(directory.FullName, filterMask); foreach (var item in result) { output.Add(item); } } return(output); }
protected virtual void OnRaiseFileFoundEvent(FileEventArgs fileEventArgs) { FileFound?.Invoke(this, fileEventArgs); }
private void OnFileFound(PathEventArgs args) { FileFound?.Invoke(this, args); }
protected void OnFileFound(object sender, FileFoundEventArgs args) { FileFound?.Invoke(sender, args); }
protected virtual void OnFileFound(SystemVisitorEventArgs e) => FileFound?.Invoke(this, e);
/// <summary> /// Executed when a file is found. /// </summary> /// <param name="file">The found file.</param> protected virtual void OnFileFound(string file) { FileFound?.Invoke(file); }
protected virtual void OnFileFound(ItemFoundInfoEventArgs e) { FileFound?.Invoke(this, e); }
public void TraverseFileSystem() { Started?.Invoke(); _dirsAndFiles = new List <FileSystemInfo>(); var dirs = new Stack <string>(); dirs.Push(_rootPath); while (dirs.Count > 0) { var currentDir = dirs.Pop(); string[] subDirs; try { subDirs = Directory.GetDirectories(currentDir); foreach (var subDir in subDirs) { var directoryInfo = new DirectoryInfo(subDir); _dirsAndFiles.Add(directoryInfo); var args = new ItemFoundEventArgs <DirectoryInfo>(directoryInfo); DirectoryFound?.Invoke(this, args); if (args.CancelRequested) { return; } dirs.Push(subDir); } } catch (Exception e) { Console.WriteLine(e.Message); continue; } string[] files; try { files = Directory.GetFiles(currentDir); foreach (var file in files) { var fileInfo = new FileInfo(file); _dirsAndFiles.Add(fileInfo); var args = new ItemFoundEventArgs <FileInfo>(fileInfo); FileFound?.Invoke(this, args); if (args.CancelRequested) { break; } } } catch (Exception e) { Console.WriteLine(e.Message); continue; } } Finished?.Invoke(); }
protected virtual void OnFileFound(string fileName) { FileFound?.Invoke(this, fileName); }