/// <summary> /// Processes all files/dirs in the top level target directory. /// </summary> /// <returns> /// Success/Failure. /// </returns> /// <param name="path">An existing file system directory.</param> public bool GetFileSystemSnapshot(string path) { List <String> directoryList = new List <String>(); WIN32_FIND_DATAW findData; IntPtr findHandle = INVALID_HANDLE_VALUE; try { path = path.EndsWith(@"\") ? path : path + @"\"; findHandle = FindFirstFileW(path + @"*", out findData); if (findHandle != INVALID_HANDLE_VALUE) { do { // Skip current directory (.) and parent directory (..) symbols if (findData.cFileName != "." && findData.cFileName != "..") { string fullPath = path + findData.cFileName; // Check if this is a directory and not a symbolic link since symbolic links // could lead to repeated files and folders as well as infinite loops bool isDirectory = findData.dwFileAttributes.HasFlag(FileAttributes.Directory); bool isSymbolicLink = findData.dwFileAttributes.HasFlag(FileAttributes.ReparsePoint); if (isDirectory && !isSymbolicLink) { // Process directory //string fullPath = path + findData.cFileName; directoryList.Add(fullPath); // Query file system entry, populate and append to dict FileInformation directoryInformation = PopulateFileInformation(findData, path); AllDirs.TryAdd(directoryInformation.FullPath, directoryInformation); AllFSEntries.TryAdd(directoryInformation.FullPath, directoryInformation); // Increase directory count Interlocked.Increment(ref DirCounter); Console.Write("\rDirs: {0} Files: {1}", DirCounter, FileCounter); } else if (!isDirectory) { // Process file FileInformation fileInformation = PopulateFileInformation(findData, path); AllFiles.TryAdd(fileInformation.FullPath, fileInformation); AllFSEntries.TryAdd(fileInformation.FullPath, fileInformation); // Increase file count Interlocked.Increment(ref FileCounter); Console.Write("\rDirs: {0} Files: {1}", DirCounter, FileCounter); } } } // Process any subdirectories while (FindNextFile(findHandle, out findData)); directoryList.AsParallel().ForAll(x => { if (FindNextFileRecursive(x)) { } }); } } catch (Exception exception) { Console.WriteLine("Caught exception while trying to enumerate a directory..."); Console.WriteLine(exception.ToString()); if (findHandle != INVALID_HANDLE_VALUE) { FindClose(findHandle); } return(false); } if (findHandle != INVALID_HANDLE_VALUE) { FindClose(findHandle); } return(true); }
/// <summary> /// Recursively processes subdirectories and the contained files. /// </summary> /// <returns> /// Success/Failure. /// </returns> /// <param name="path">An existing file system directory.</param> /// <param name="files">List of files to process.</param> /// <param name="directories">List of directories to process.</param> private bool FindNextFileRecursive(string path) { List <String> directoryList = new List <String>(); WIN32_FIND_DATAW findData; IntPtr findHandle = INVALID_HANDLE_VALUE; try { findHandle = FindFirstFileW(path + @"\*", out findData); if (findHandle != INVALID_HANDLE_VALUE) { do { // Skip current directory (.) and parent directory (..) symbols if (findData.cFileName != "." && findData.cFileName != "..") { string fullPath = path + @"\" + findData.cFileName; // Check if this is a directory and not a symbolic link since symbolic links // could lead to repeated files and folders as well as infinite loops. bool isDirectory = findData.dwFileAttributes.HasFlag(FileAttributes.Directory); bool isSymbolicLink = findData.dwFileAttributes.HasFlag(FileAttributes.ReparsePoint); if (isDirectory && !isSymbolicLink) { // Add the directory to the list directoryList.Add(fullPath); FileInformation directoryInformation = PopulateFileInformation(findData, path); AllDirs.TryAdd(directoryInformation.FullPath, directoryInformation); AllFSEntries.TryAdd(directoryInformation.FullPath, directoryInformation); // Increase directory count Interlocked.Increment(ref DirCounter); Console.Write("\rDirs: {0} Files: {1}", DirCounter, FileCounter); if (FindNextFileRecursive(fullPath)) { } } else if (!isDirectory) { // Add the file to the list FileInformation fileInformation = PopulateFileInformation(findData, path); AllFiles.TryAdd(fileInformation.FullPath, fileInformation); AllFSEntries.TryAdd(fileInformation.FullPath, fileInformation); // Increase file count Interlocked.Increment(ref FileCounter); Console.Write("\rDirs: {0} Files: {1}", DirCounter, FileCounter); } } }while (FindNextFile(findHandle, out findData)); } } catch (Exception exception) { Console.WriteLine("Caught exception while trying to enumerate a directory..."); Console.WriteLine(exception.ToString()); if (findHandle != INVALID_HANDLE_VALUE) { FindClose(findHandle); } return(false); } if (findHandle != INVALID_HANDLE_VALUE) { FindClose(findHandle); } return(true); }