/// <summary> /// Walks the directory tree. /// </summary> /// <param name="oneTimeRoot">The one time root.</param> /// <param name="fileAction">The file action.</param> /// <param name="directoryAction">The directory action.</param> /// <param name="errorAction">The error action.</param> /// <param name="searchPattern">The search pattern.</param> public static void WalkDirectoryTree(DirectoryInfo oneTimeRoot, FileStepping fileAction, DirectoryStepping directoryAction, HandleSteppingError errorAction, string searchPattern = null) { FileInfo[] files = null; // First, process all the files directly under this folder try{ files = oneTimeRoot.GetFiles(string.IsNullOrEmpty(searchPattern) ? DefaultPattern : searchPattern); } catch (Exception e) { errorAction(new StepErrorEventArgs(e));// fire the error handler function } if (files != null) { foreach (FileInfo fi in files) { // In this example, we only access the existing FileInfo object. If we // want to open, delete or modify the file, then // a try-catch block is required here to handle the case // where the file has been deleted since the call to TraverseTree(). try { fileAction(new FileStepEventArgs(fi)); } catch (Exception e) { errorAction(new StepErrorEventArgs(e)); // fire the error handler function } } // Now find all the subdirectories under this directory. IEnumerable <DirectoryInfo> subDirs = oneTimeRoot.GetDirectories(); foreach (DirectoryInfo dirInfo in subDirs) { // fire the directory action function directoryAction(new DirectoryStepEventArgs(dirInfo)); // Resursive call for each subdirectory. WalkDirectoryTree(dirInfo, fileAction, directoryAction, errorAction, searchPattern); } } }
/// <summary> /// Walks the directory tree. /// </summary> /// <param name="oneTimeRoot">The one time root.</param> /// <param name="fileAction">The file action.</param> /// <param name="directoryAction">The directory action.</param> /// <param name="errorAction">The error action.</param> /// <param name="searchPattern">The search pattern.</param> public static void WalkDirectoryTree(DirectoryInfo oneTimeRoot, FileStepping fileAction, DirectoryStepping directoryAction, HandleSteppingError errorAction, string searchPattern = null) { FileInfo[] files = null; // First, process all the files directly under this folder try{ files = oneTimeRoot.GetFiles( string.IsNullOrEmpty( searchPattern ) ? DefaultPattern : searchPattern ); } catch ( Exception e ) { errorAction(new StepErrorEventArgs(e));// fire the error handler function } if (files != null) { foreach (FileInfo fi in files) { // In this example, we only access the existing FileInfo object. If we // want to open, delete or modify the file, then // a try-catch block is required here to handle the case // where the file has been deleted since the call to TraverseTree(). try { fileAction(new FileStepEventArgs(fi)); } catch (Exception e) { errorAction( new StepErrorEventArgs( e ) );// fire the error handler function } } // Now find all the subdirectories under this directory. IEnumerable< DirectoryInfo > subDirs = oneTimeRoot.GetDirectories(); foreach (DirectoryInfo dirInfo in subDirs){ // fire the directory action function directoryAction( new DirectoryStepEventArgs( dirInfo ) ); // Resursive call for each subdirectory. WalkDirectoryTree( dirInfo, fileAction, directoryAction, errorAction, searchPattern ); } } }
/// <summary> /// Walks the directory tree. /// </summary> /// <param name="oneTimeRootPath">The one time root path.</param> /// <param name="fileAction">The file action.</param> /// <param name="directoryAction">The directory action.</param> /// <param name="errorAction">The error action.</param> /// <param name="searchPattern">The search pattern.</param> public static void WalkDirectoryTree(string oneTimeRootPath, FileStepping fileAction, DirectoryStepping directoryAction, HandleSteppingError errorAction, string searchPattern = null) { WalkDirectoryTree(new DirectoryInfo(oneTimeRootPath), fileAction, directoryAction, errorAction, searchPattern); }