/// <summary> /// Recursive scanning for files and folders /// Uses DirectoryInfo.GetFileSystemInfos method to simultaneously get folder and file information /// </summary> /// <param name="folder"> /// Folder to be scanned for /// Recursively adds subfolders and file information to the folder /// </param> void GetFilesAndFolders(AppFolder folder) { var searchDir = new DirectoryInfo(folder.FullPath); FileSystemInfo[] filesAndFolders = null; try { if ((searchDir.Attributes & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint) { filesAndFolders = searchDir.GetFileSystemInfos(); } } catch { // Avoid Access Denied exceptions } if (filesAndFolders != null) { foreach (FileSystemInfo fileInfo in filesAndFolders) { if ((fileInfo.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint) { continue; } BwFolderState folderStateData = null; Type t = fileInfo.GetType(); if (t == typeof(DirectoryInfo)) { var dir = fileInfo as DirectoryInfo; var subFolder = new AppFolder { Name = dir.Name, FullPath = dir.FullName }; folderStateData = new BwFolderState { CurrentFolder = folder, SubFolder = subFolder }; if (!bwDiskScanner.CancellationPending) { int prog; prog = totalFileSizeToScan != 0 ? Convert.ToInt32((scannedFileSize * 100) / totalFileSizeToScan) : 1; bwDiskScanner.ReportProgress(prog, folderStateData); GetFilesAndFolders(subFolder); } } else if (t == typeof(FileInfo)) { var file = fileInfo as FileInfo; AppFile af = null; try { if (file != null) af = new AppFile { FileName = file.Name, FolderPath = file.DirectoryName, LFileSize = file.Length, ModifiedDate = file.LastWriteTime, FileType = file.Extension.ToLower(), Attributes = file.Attributes.ToString() }; } catch (Exception ex) { LogClass.AddErrorToLog(" Method - GetFilesAndFolders - Exeption [" + ex.GetType().Name + "] - " + ex.Message); try { if (file != null) af = new AppFile { FileName = file.Name, FolderPath = file.DirectoryName, LFileSize = file.Length, FileType = file.Extension.ToLower(), Attributes = file.Attributes.ToString() }; } catch (Exception ex2) { LogClass.AddErrorToLog(" Method - GetFilesAndFolders - Exeption [" + ex2.GetType().Name + "] - " + ex2.Message); } } try { if (af != null) folder.Files.Add(af); if (file != null) scannedFileSize += file.Length; } catch (Exception ex) { LogClass.AddErrorToLog(" Method - GetFilesAndFolders - Exeption [" + ex.GetType().Name + "] - " + ex.Message); } } } } }
/// <summary> /// Adds a file to the files collection /// </summary> /// <param name="file"></param> public void Add(AppFile file) { Files.Add(file); }
void StartWork() { LogClass.AddInfoToLog(LogClass.LogInfo.Start, "StartWork"); Analyze.IsEnabled = false; if (d != null) { // Initialize the Root folders and files // And then call background worker var f = new AppFolder { Name = d.Name, FullPath = d.FullName, IsRootDirectory = true }; directories.Add(f); FileSystemInfo[] filesNFolders = new FileSystemInfo[] { }; try { filesNFolders = d.GetFileSystemInfos(); } catch (Exception ex) { LogClass.AddErrorToLog(" Method - ControlSortingAbility - Exeption [" + ex.GetType().Name + "] - " + ex.Message); } foreach (FileSystemInfo fileInfo in filesNFolders) { if ((fileInfo.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint) { continue; } Type t = fileInfo.GetType(); if (t == typeof(DirectoryInfo)) { var dir = fileInfo as DirectoryInfo; if (dir != null) f.SubFolders.Add(new AppFolder { Name = dir.Name, FullPath = dir.FullName }); } else if (t == typeof(FileInfo)) { var file = fileInfo as FileInfo; if (file != null) { var af = new AppFile { FileName = file.Name, FolderPath = file.DirectoryName, LFileSize = file.Length, ModifiedDate = file.LastWriteTime, FileType = file.Extension.ToLower(), Attributes = file.Attributes.ToString() }; f.Files.Add(af); } } } txtStatus.Text = Resx.Scanning; progressBar1.Visibility = Visibility.Visible; progressBar1.IsIndeterminate = false; lnkAbort.Visibility = Visibility.Visible; bwDiskScanner.RunWorkerAsync(); } LogClass.AddInfoToLog(LogClass.LogInfo.End, "StartWork"); }
/// <summary> /// Starts analyzing work /// </summary> void StartWork() { EnableButtons(false); ; if (d != null) { // Initialize the Root folders and files // And then call background worker AppFolder f = new AppFolder { Name = d.Name, FullPath = d.FullName, IsRootDirectory = true }; directories.Add(f); FileSystemInfo[] filesNFolders = new FileSystemInfo[] { }; try { filesNFolders = d.GetFileSystemInfos(); } catch { } foreach (FileSystemInfo fileInfo in filesNFolders) { if ((fileInfo.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint) { continue; } Type t = fileInfo.GetType(); if (t == typeof(DirectoryInfo)) { DirectoryInfo dir = fileInfo as DirectoryInfo; if (dir != null) f.SubFolders.Add(new AppFolder { Name = dir.Name, FullPath = dir.FullName }); } else if (t == typeof(FileInfo)) { FileInfo file = fileInfo as FileInfo; if (file != null) { AppFile af = new AppFile { FileName = file.Name, FolderPath = file.DirectoryName, LFileSize = file.Length, ModifiedDate = file.LastWriteTime, FileType = file.Extension.ToLower(), Attributes = file.Attributes.ToString() }; f.Files.Add(af); } } } txtStatus.Text = Resx.Scanning; progressBar1.Visibility = Visibility.Visible; progressBar1.IsIndeterminate = false; lnkAbort.Visibility = Visibility.Visible; bwDiskScanner.RunWorkerAsync(); } }