public SubdirScanner( string dir ) { DirectoryInfo dirInfo = new DirectoryInfo( dir ); // This array may be QUITE large, some people put all files // in one place. :/ _files = dirInfo.GetFiles(); _fileIndex = 0; _subdirs = Directory.GetDirectories( dir ); _subdirIndex = 0; // Create first subdir, if any subdirs exist if (_subdirs.Length > 0) _currentSubdir = new SubdirScanner( _subdirs[0] ); }
/// /// \param file is returned set to the next file from this /// search, or not modified if there are no more files. /// /// \return false if no more files in this dir, and no more /// subdirs to process /// public bool GetNext( out string file ) { // Get the next file in this dir, or // get the next file from the current subdir, or // goto the next subdir // If no more subdirs, we're done if (_fileIndex < _files.Length) { file = _files[_fileIndex].FullName; ++ _fileIndex; return true; } else { // Scan through subdirs until a file is found or no more // subdirs exist: if (_subdirIndex >= _subdirs.Length) { file = null; return false; // No more subdirs! } while (true) { Debug.Assert( null != _currentSubdir, "logic error" ); if (_currentSubdir.GetNext( out file )) return true; // If we got here, the current subdir is finished ++ _subdirIndex; if (_subdirIndex >= _subdirs.Length) return false; _currentSubdir = new SubdirScanner( _subdirs[_subdirIndex] ); } } // not reached }
/// /// Resets the scanner to the first file /// public void Rewind() { _scanner = new SubdirScanner( _rootDir ); }