void GetFileTypeData(AppFolder f) { foreach (AppFile file in f.Files) { IEnumerable<AppFileType> isExists = (from ft in fileTypeList where ft.FileType == file.FileType.ToLower() select ft); if (isExists.Count() == 1) { isExists.First().Add(file); } else { var fileType = new AppFileType(file.FileType); fileType.Add(file); fileTypeList.Add(fileType); } } foreach (AppFolder folder in f.SubFolders) { GetFileTypeData(folder); } }
void GetPercentage(AppFolder fol, long totalSize) { decimal d = Convert.ToDecimal(fol.LSize * 100.0); fol.DPercent = totalSize == 0 ? 100 : decimal.Round(d / totalSize, 2); foreach (AppFolder fol1 in fol.SubFolders) { GetPercentage(fol1, totalSize); } }
/// <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); } } } } }
void Delete_Click(object sender, RoutedEventArgs e) { LogClass.AddInfoToLog(LogClass.LogInfo.Start, "Delete_Click"); if (lastFocusedList == rtvFolderList && rtvFolderList.SelectedItem != null) { var selectedFolder = rtvFolderList.SelectedItem as AppFolder; var d = new DirectoryInfo(selectedFolder.FullPath); MessageBoxResult result = MessageBox.Show(Resx.DeleteFolderConfirmation.Replace("{1}", d.Name), Resx.DeleteFolder, MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { try { d.Delete(true); FindAndDeleteDirectory(d, directories); dgFileListByFolder.UpdateLayout(); } catch (Exception ex) { MessageBox.Show(Resx.NoRightsToDelete, Resx.DiskAnalysis, MessageBoxButton.OK, MessageBoxImage.Error); LogClass.AddErrorToLog(" Method - Delete_Click - Exeption [" + ex.GetType().Name + "] - " + ex.Message); } } } else if (lastFocusedList == dgFileListByFolder && dgFileListByFolder.SelectedItem != null) { var selectedFile = dgFileListByFolder.SelectedItem as AppFile; if (selectedFile != null) { var f = new FileInfo(selectedFile.FilePath); MessageBoxResult result = MessageBox.Show(Resx.DeleteFileConfirmation.Replace("{1}", selectedFile.FileName), Resx.DeleteFile, MessageBoxButton.YesNo, MessageBoxImage.Question); string tmp = f.Attributes.ToString(); MessageBoxResult qSystem; MessageBoxResult qHidden; if (f.Attributes.ToString().Contains("System")) { qSystem = MessageBox.Show(Resx.DeleteSystemFile, Resx.DeleteFile, MessageBoxButton.YesNo, MessageBoxImage.Warning); } else { qSystem = MessageBoxResult.Yes; } if ((result == MessageBoxResult.Yes) && f.Attributes.ToString().Contains("Hidden")) { qHidden = MessageBox.Show(Resx.DeleteHiddenFile, Resx.DeleteFile, MessageBoxButton.YesNo, MessageBoxImage.Warning); } else { qHidden = MessageBoxResult.Yes; } //MessageBoxResult result = MessageBox.Show(Resx.DeleteFileConfirmation + " " + selectedFile.FileName + "?", // Resx.DeleteFile, MessageBoxButton.YesNo, MessageBoxImage.Question); if ((result == MessageBoxResult.Yes) && (qSystem == MessageBoxResult.Yes) && (qHidden == MessageBoxResult.Yes)) { int fileIndex = GetFileIndex(f); if (fileIndex != -1) { try { var selectedFolder = rtvFolderList.SelectedItem as AppFolder; f.Delete(); if (selectedFolder != null) { AppFile fileToRemove = selectedFolder.Files.Where(fl => fl.FileName == f.Name).FirstOrDefault(); // Delete the file from rtvFolderList itemsource selectedFolder.Files.Remove(fileToRemove); } if (selectedFolder != null) dgFileListByFolder.ItemsSource = selectedFolder.Files; dgFileListByFolder.UpdateLayout(); // AppFolder fol = directories.Where(dir=>dir.FullPath == f.DirectoryName).FirstOrDefault(); // string tmp = fol.FilesCount +""; //need to delete from directories, the same file being deleted in the rest of the lists // the file is f , directories is the obsev collection // filepath is full path string filePath = f.DirectoryName; var currentDir = new AppFolder { SubFolders = directories }; while (currentDir != null && currentDir.FullPath != filePath) { currentDir = currentDir.SubFolders.FirstOrDefault(s => filePath != null && filePath.StartsWith(s.FullPath)); } if (currentDir != null) { AppFile tobeRem = currentDir.Files.Where(file => file.FileName == f.Name).FirstOrDefault(); currentDir.Files.Remove(tobeRem); } rtvFolderList.ItemsSource = directories; rtvFolderList.UpdateLayout(); // currentDir here contains the file //dgFileListByFolder.ItemsSource = selectedFolder.Files.OrderByDescending(files => files.lFileSize); //dgFileListByFolder.UpdateLayout(); AppFileType extensionList = fileTypeList.Where(ext => ext.FileType == f.Extension.ToLower()).FirstOrDefault(); if (extensionList != null) { List<AppFile> extensionFiles = extensionList.Files; AppFile fileToRemoveExt = extensionFiles.Where(fl => fl.FolderPath == f.DirectoryName && fl.FileName == f.Name).FirstOrDefault(); extensionFiles.Remove(fileToRemoveExt); } //dgFileTypeList.UnselectAll(); //remove it from the below grid if (rtvFolderList.SelectedItem == null) { if (extensionList != null) dgFileListByFolder.ItemsSource = extensionList.Files.OrderByDescending(file => file.LFileSize); dgFileListByFolder.UpdateLayout(); } dgFileTypeList.ItemsSource = fileTypeList.OrderByDescending(files => files.lTotalFileSize); dgFileTypeList.UpdateLayout(); // TODO: Refreshing the info from correspondent element } catch (UnauthorizedAccessException) { MessageBox.Show(Resx.NoRightsToDelete, Resx.DiskAnalysis, MessageBoxButton.OK, MessageBoxImage.Error); } catch (System.IO.IOException exception) { MessageBox.Show(Resx.FileInUse, Resx.DiskAnalysis, MessageBoxButton.OK, MessageBoxImage.Error); LogClass.AddErrorToLog(" Method - Delete_Click - Exeption [" + exception.GetType().Name + "] - " + exception.Message); } catch (Exception ex) { LogClass.AddErrorToLog(" Method - Delete_Click - Exeption [" + ex.GetType().Name + "] - " + ex.Message); } } } } } LogClass.AddInfoToLog(LogClass.LogInfo.End, "Delete_Click"); }
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> /// Handles click event of delete button /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Delete_Click(object sender, RoutedEventArgs e) { try { if (lastFocusedList == rtvFolderList && rtvFolderList.SelectedItem != null) { AppFolder selectedFolder = rtvFolderList.SelectedItem as AppFolder; try { DirectoryInfo d = new DirectoryInfo(selectedFolder.FullPath); MessageBoxResult result = MessageBox.Show(Resx.DeleteFolderConfirmation.Replace("{1}", d.Name), Resx.DeleteFolder, MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { try { d.Delete(true); FindAndDeleteDirectory(d, directories); dgFileListByFolder.UpdateLayout(); } catch { MessageBox.Show(Resx.NoRightsToDelete, Resx.DiskAnalysis, MessageBoxButton.OK, MessageBoxImage.Error); } } } catch { } } else if (lastFocusedList == dgFileListByFolder && dgFileListByFolder.SelectedItem != null) { AppFile selectedFile = dgFileListByFolder.SelectedItem as AppFile; if (selectedFile != null) { FileInfo f = new FileInfo(selectedFile.FilePath); MessageBoxResult result = MessageBox.Show(Resx.DeleteFileConfirmation.Replace("{1}", selectedFile.FileName), Resx.DeleteFile, MessageBoxButton.YesNo, MessageBoxImage.Question); string tmp = f.Attributes.ToString(); MessageBoxResult qSystem; MessageBoxResult qHidden; if (f.Attributes.ToString().Contains("System")) { qSystem = MessageBox.Show(Resx.DeleteSystemFile, Resx.DeleteFile, MessageBoxButton.YesNo, MessageBoxImage.Warning); } else { qSystem = MessageBoxResult.Yes; } if ((result == MessageBoxResult.Yes) && f.Attributes.ToString().Contains("Hidden")) { qHidden = MessageBox.Show(Resx.DeleteHiddenFile, Resx.DeleteFile, MessageBoxButton.YesNo, MessageBoxImage.Warning); } else { qHidden = MessageBoxResult.Yes; } if ((result == MessageBoxResult.Yes) && (qSystem == MessageBoxResult.Yes) && (qHidden == MessageBoxResult.Yes)) { int fileIndex = GetFileIndex(f); if (fileIndex != -1) { try { AppFolder selectedFolder = rtvFolderList.SelectedItem as AppFolder; f.Delete(); if (selectedFolder != null) { AppFile fileToRemove = selectedFolder.Files.Where(fl => fl.FileName == f.Name).FirstOrDefault(); // Delete the file from rtvFolderList itemsource selectedFolder.Files.Remove(fileToRemove); } if (selectedFolder != null) dgFileListByFolder.ItemsSource = selectedFolder.Files; dgFileListByFolder.UpdateLayout(); string filePath = f.DirectoryName; AppFolder currentDir = new AppFolder { SubFolders = directories }; while (currentDir != null && currentDir.FullPath != filePath) { currentDir = currentDir.SubFolders.FirstOrDefault(s => filePath != null && filePath.StartsWith(s.FullPath)); } if (currentDir != null) { AppFile tobeRem = currentDir.Files.Where(file => file.FileName == f.Name).FirstOrDefault(); currentDir.Files.Remove(tobeRem); } rtvFolderList.ItemsSource = directories; rtvFolderList.UpdateLayout(); // currentDir here contains the file AppFileType extensionList = fileTypeList.Where(ext => ext.FileType == f.Extension.ToLower()).FirstOrDefault(); if (extensionList != null) { List<AppFile> extensionFiles = extensionList.Files; AppFile fileToRemoveExt = extensionFiles.Where(fl => fl.FolderPath == f.DirectoryName && fl.FileName == f.Name).FirstOrDefault(); extensionFiles.Remove(fileToRemoveExt); } //remove it from the below grid if (rtvFolderList.SelectedItem == null) { if (extensionList != null) dgFileListByFolder.ItemsSource = extensionList.Files.OrderByDescending(file => file.LFileSize); dgFileListByFolder.UpdateLayout(); } dgFileTypeList.ItemsSource = fileTypeList.OrderByDescending(files => files.lTotalFileSize); dgFileTypeList.UpdateLayout(); // TODO: Refreshing the info from correspondent element } catch (UnauthorizedAccessException) { MessageBox.Show(Resx.NoRightsToDelete, Resx.DiskAnalysis, MessageBoxButton.OK, MessageBoxImage.Error); } catch (System.IO.IOException exception) { MessageBox.Show(Resx.FileInUse, Resx.DiskAnalysis, MessageBoxButton.OK, MessageBoxImage.Error); } catch { } } } } } } catch { } }
/// <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(); } }