CleanHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. namespace Optimizer
  7. {
  8. internal static class CleanHelper
  9. {
  10. [DllImport("Shell32.dll")]
  11. static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlag dwFlags);
  12. internal static readonly string System32Folder = Environment.GetFolderPath(Environment.SpecialFolder.System);
  13. internal static readonly string TempFolder = Path.GetTempPath();
  14. internal static readonly string ProfileAppDataRoaming = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  15. internal static readonly string ProgramData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
  16. internal static readonly string ProfileAppDataLocal = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
  17. //internal static readonly string ProfileAppDataLocalLow = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "Low";
  18. //internal static readonly string OSDrive = System32Folder.Substring(0, 3);
  19. internal static readonly string OSDriveWindows = Environment.GetEnvironmentVariable("WINDIR", EnvironmentVariableTarget.Machine);
  20. internal static List<string> PreviewCleanList = new List<string>();
  21. internal static void PreviewFolder(string path)
  22. {
  23. try
  24. {
  25. DirectoryInfo di = new DirectoryInfo(path);
  26. foreach (FileInfo file in di.GetFiles("*", SearchOption.AllDirectories))
  27. {
  28. try
  29. {
  30. PreviewCleanList.Add(file.FullName);
  31. }
  32. catch { }
  33. }
  34. foreach (DirectoryInfo dir in di.GetDirectories("*", SearchOption.AllDirectories))
  35. {
  36. try
  37. {
  38. PreviewCleanList.Add(dir.FullName);
  39. }
  40. catch { }
  41. }
  42. }
  43. catch { }
  44. }
  45. internal static void Clean()
  46. {
  47. foreach (string x in PreviewCleanList)
  48. {
  49. try
  50. {
  51. if (Directory.Exists(x)) Directory.Delete(x);
  52. if (File.Exists(x)) File.Delete(x);
  53. }
  54. catch { }
  55. }
  56. }
  57. internal static void EmptyRecycleBin()
  58. {
  59. SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlag.SHERB_NOSOUND | RecycleFlag.SHERB_NOCONFIRMATION);
  60. }
  61. internal static void PreviewTemp()
  62. {
  63. PreviewFolder(TempFolder);
  64. }
  65. internal static void PreviewMinidumps()
  66. {
  67. PreviewFolder(OSDriveWindows + "\\Minidump");
  68. }
  69. internal static void PreviewErrorReports()
  70. {
  71. PreviewFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ReportArchive");
  72. PreviewFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ReportQueue");
  73. PreviewFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\Temp");
  74. PreviewFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ERC");
  75. PreviewFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ReportArchive");
  76. PreviewFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ReportQueue");
  77. PreviewFolder(ProgramData + "\\Microsoft\\Windows\\WER\\Temp");
  78. PreviewFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ERC");
  79. }
  80. // only for TEMP folder
  81. internal static ByteSize CheckFootprint()
  82. {
  83. try
  84. {
  85. DirectoryInfo info = new DirectoryInfo(TempFolder);
  86. return ByteSize.FromBytes(info.EnumerateFiles("*", SearchOption.AllDirectories).Sum(file => file.Length));
  87. }
  88. catch (Exception ex)
  89. {
  90. ErrorLogger.LogError("CleanHelper.CleanLogs", ex.Message, ex.StackTrace);
  91. return ByteSize.FromBytes(0);
  92. }
  93. }
  94. }
  95. }