CleanHelper.cs 4.2 KB

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