// --------------------------------------------------------------------------------------------------------------------- public void RemoveFilesFromTemporaryList() { this.UpdateProgressBar(0.0, 0.0); long lnCurrentFreeSpace = 0; var laDrives = DriveInfo.GetDrives(); foreach (var loDrive in laDrives) { if (loDrive.DriveType == DriveType.Fixed) { lnCurrentFreeSpace += loDrive.TotalFreeSpace; } } this.UpdateListBox(""); long lnTotalSize = 0; // First display any files that have, for whatever reason, // already been removed. foreach (var lcFile in this.foTemporaryFileList.Values) { if (!File.Exists(lcFile) && !Directory.Exists(lcFile)) { this.UpdateListBox("Already removed " + lcFile); } } // Now get rid of the files if possible. foreach (var lcFile in this.foTemporaryFileList.Values) { if (Directory.Exists(lcFile)) { continue; } try { if (File.Exists(lcFile)) { var loFileInfo = new FileInfo(lcFile); var lnSizeTemp = loFileInfo.Length; var llReadOnly = (File.GetAttributes(lcFile) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly; if (llReadOnly) { File.SetAttributes(lcFile, FileAttributes.Normal); } File.Delete(lcFile); this.UpdateListBox("Removed " + lcFile); // If you haven't already determined the file size, and the file is deleted, then // the size will be zero. lnTotalSize += lnSizeTemp; } } catch (Exception) { this.UpdateListBox("Unable to remove file " + lcFile); } } // Now get rid of the sub-directories if possible. foreach (var lcFile in this.foTemporaryFileList.Values) { if (Directory.Exists(lcFile)) { try { Directory.Delete(lcFile); this.UpdateListBox("Removed directory " + lcFile); } catch (Exception) { this.UpdateListBox("Unable to remove directory " + lcFile); } } } var llRecycleBin = this.foMainWindow.UserSettings.GetMainFormUseRecycleBin(); if (llRecycleBin) { RecycleBin.EmptyRecycleBin(); var loInfo = RecycleBin.GetRecycleBinInfo(); var lcError = RecycleBin.GetLastError(); this.UpdateListBox("Recycle Bin Info:"); this.UpdateListBox(ThreadRoutines.INDENT + "File(s) and/or folder(s) in root folder: " + loInfo.Items); this.UpdateListBox(ThreadRoutines.INDENT + "Byte(s) in root folder: " + Util.FormatBytes_GB_MB_KB(loInfo.Bytes) + " (" + Util.FormatBytes_Actual(loInfo.Bytes) + ")"); if (lcError.Length != 0) { this.UpdateListBox("There was an error reading the Recycle Bin: (Result# " + RecycleBin.GetLastResult() + ") " + lcError); } } this.UpdateListBox(""); this.UpdateListBox("Total Temporary Files Removed: " + Util.FormatBytes_GB_MB_KB(lnTotalSize) + " (" + Util.FormatBytes_Actual(lnTotalSize) + ")"); this.UpdateListBox(""); laDrives = DriveInfo.GetDrives(); long lnNewFreeSpace = 0; foreach (var loDrive in laDrives) { if (loDrive.DriveType == DriveType.Fixed) { var lnFree = loDrive.TotalFreeSpace; lnNewFreeSpace += lnFree; this.UpdateListBox(loDrive.RootDirectory + " (free space): " + Util.FormatBytes_GB_MB_KB(lnFree) + " (" + Util.FormatBytes_Actual(lnFree) + ")"); } } this.UpdateListBox(""); this.UpdateListBox("Total Removed (Temporary Files" + (llRecycleBin ? " & Recycle Bin" : " only") + "):"); this.UpdateListBox(ThreadRoutines.INDENT + Util.FormatBytes_GB_MB_KB(lnNewFreeSpace - lnCurrentFreeSpace) + " (" + Util.FormatBytes_Actual(lnNewFreeSpace - lnCurrentFreeSpace) + ")"); this.UpdateListBox(""); this.UpdateListBox("The operation has successfully completed.", true); // Guarantee 100% for progress bar. this.UpdateProgressBar(100.0, 100.0); this.StartResetProgressBar(); }
// --------------------------------------------------------------------------------------------------------------------- public void UpdateControlForTemporary() { var loMainWindow = this.foMainWindow; this.UpdateProgressBar(0.0, 0.0); this.UpdateListBox(""); this.UpdateListBox("The following files were located:"); this.UpdateListBox(""); long lnTotalSize = 0; while (loMainWindow.IsThreadRunning()) { var loFileData = this.FileInformationForTemporary.XmlFileInformation.ReadFileData(); if (loFileData == null) { break; } var llFolder = loFileData.IsFolder; var lnFolderLevel = loFileData.FolderLevel; var lcFile = loFileData.FullName; // Do NOT add the root directories to this list: they need to remain on the hard drive. if (llFolder && (lnFolderLevel == 0)) { lnTotalSize += loFileData.Size; continue; } // Doesn't matter if an element (or file) with the same key already exists in the list. try { this.foTemporaryFileList.Add(lcFile, lcFile); } catch (ArgumentException) { } var llReadOnly = (loFileData.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly; this.UpdateListBox(lcFile + (llReadOnly ? " (Read Only)" : "")); } this.UpdateListBox(""); this.UpdateListBox("Total Temporary Files: " + Util.FormatBytes_GB_MB_KB(lnTotalSize) + " (" + Util.FormatBytes_Actual(lnTotalSize) + ")"); this.UpdateListBox(""); var llRecycleBin = this.foMainWindow.UserSettings.GetMainFormUseRecycleBin(); if (llRecycleBin) { var loInfo = RecycleBin.GetRecycleBinInfo(); var lcError = RecycleBin.GetLastError(); this.UpdateListBox("Recycle Bin Info:"); this.UpdateListBox(ThreadRoutines.INDENT + "File(s) and/or folder(s) in root folder: " + loInfo.Items); this.UpdateListBox(ThreadRoutines.INDENT + "Byte(s) in root folder: " + Util.FormatBytes_GB_MB_KB(loInfo.Bytes) + " (" + Util.FormatBytes_Actual(loInfo.Bytes) + ")"); if (lcError.Length != 0) { this.UpdateListBox("There was an error reading the Recycle Bin: (Result# " + RecycleBin.GetLastResult() + ") " + lcError); } this.UpdateListBox(""); } var laDrives = DriveInfo.GetDrives(); foreach (var loDrive in laDrives) { if (loDrive.DriveType == DriveType.Fixed) { this.UpdateListBox(loDrive.RootDirectory + " (free space): " + Util.FormatBytes_GB_MB_KB(loDrive.TotalFreeSpace) + " (" + Util.FormatBytes_Actual(loDrive.TotalFreeSpace) + ")"); } } this.UpdateListBox(""); this.UpdateListBox("Press the Remove button to delete the above listed files.", true); // Guarantee 100% for progress bar. this.UpdateProgressBar(100.0, 100.0); this.StartResetProgressBar(); }