/// <summary> /// Scan a directory for viruses, optionally recursing into subdirectories. /// </summary> /// <param name="directoryPath">Path to scan.</param> /// <param name="scanOptions">Scan options.</param> /// <param name="fileScannedCallback">Called after a file has been scanned.</param> /// <param name="recurse">Enter subdirectories.</param> /// <param name="maxDepth">Maximum depth to scan, or zero for unlimited.</param> public void ScanDirectory(string directoryPath, ScanOptions scanOptions, FileScannedCallback fileScannedCallback, bool recurse, int maxDepth) { // Validate arguments. if (string.IsNullOrEmpty(directoryPath)) { throw new ArgumentNullException(nameof(directoryPath)); } if (fileScannedCallback == null) { throw new ArgumentNullException(nameof(fileScannedCallback)); } if (maxDepth < 0) { throw new ArgumentException("maxDepth must be 0 or greater."); } var pathStack = new Stack <Tuple <string /* path */, int /* depth */> >(); // Push the starting directory onto the stack. pathStack.Push(Tuple.Create(directoryPath, 1)); while (pathStack.Count > 0) { var stackState = pathStack.Pop(); var currentPath = stackState.Item1; var currentDepth = stackState.Item2; var attributes = File.GetAttributes(currentPath); // If we're in a directory, push all files and subdirectories to the stack. if ((attributes & FileAttributes.Directory) == FileAttributes.Directory) { // Check if we're not about to go too deep. if (maxDepth == 0 || currentDepth < maxDepth) { var subFiles = Directory.GetFiles(currentPath); foreach (var file in subFiles) { pathStack.Push(Tuple.Create(file, currentDepth + 1)); } var subDirectories = Directory.GetDirectories(currentPath); foreach (var directory in subDirectories) { pathStack.Push(Tuple.Create(directory, currentDepth + 1)); } } } // If this is a file, scan it. else { var virusName = string.Empty; var scanResult = ScanFile(currentPath, scanOptions, out virusName); fileScannedCallback(currentPath, scanResult, virusName); } } }
/// <summary> /// Scan a directory for viruses, optionally recursing into subdirectories. /// </summary> /// <param name="directoryPath">Path to scan.</param> /// <param name="scanOptions">Scan options.</param> /// <param name="fileScannedCallback">Called after a file has been scanned.</param> /// <param name="recurse">Enter subdirectories.</param> /// <param name="maxDepth">Maximum depth to scan, or zero for unlimited.</param> public void ScanDirectory(string directoryPath, ScanOptions scanOptions, FileScannedCallback fileScannedCallback, bool recurse, int maxDepth) { // Validate arguments. if (string.IsNullOrEmpty(directoryPath)) throw new ArgumentNullException("directoryPath"); if (fileScannedCallback == null) throw new ArgumentNullException("fileScannedCallback"); var pathStack = new Stack<Tuple<string /* path */, int /* depth */>>(); // Push the starting directory onto the stack. pathStack.Push(Tuple.Create(directoryPath, 1)); while (pathStack.Count > 0) { var stackState = pathStack.Pop(); var currentPath = stackState.Item1; var currentDepth = stackState.Item2; var attributes = File.GetAttributes(currentPath); // If we're in a directory, push all files and subdirectories to the stack. if ((attributes & FileAttributes.Directory) == FileAttributes.Directory) { // Check if we're not about to go too deep. if (maxDepth == 0 || currentDepth < maxDepth) { var subFiles = Directory.GetFiles(currentPath); foreach (var file in subFiles) { pathStack.Push(Tuple.Create(file, currentDepth + 1)); } var subDirectories = Directory.GetDirectories(currentPath); foreach (var directory in subDirectories) { pathStack.Push(Tuple.Create(directory, currentDepth + 1)); } } } // If this is a file, scan it. else { var virusName = string.Empty; var scanResult = ScanFile(currentPath, scanOptions, out virusName); fileScannedCallback(currentPath, scanResult, virusName); } } }
/// <summary> /// Scan a directory for viruses with custom scan options, recursing into subdirectories. /// </summary> /// <param name="directoryPath">Path to scan.</param> /// <param name="scanOptions">Scan options.</param> /// <param name="fileScannedCallback">Called after a file has been scanned.</param> public void ScanDirectory(string directoryPath, ScanOptions scanOptions, FileScannedCallback fileScannedCallback) { ScanDirectory(directoryPath, ScanOptions.StandardOptions, fileScannedCallback, true, 0); }