CleanHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Threading.Tasks;
  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 void EmptyFolder(string path)
  21. {
  22. try
  23. {
  24. DirectoryInfo di = new DirectoryInfo(path);
  25. foreach (FileInfo file in di.GetFiles())
  26. {
  27. try
  28. {
  29. file.IsReadOnly = false;
  30. file.Delete();
  31. }
  32. catch { }
  33. }
  34. foreach (DirectoryInfo dir in di.GetDirectories())
  35. {
  36. try
  37. {
  38. dir.Delete(true);
  39. }
  40. catch { }
  41. }
  42. }
  43. catch { }
  44. }
  45. internal static void EmptyRecycleBin()
  46. {
  47. SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlag.SHERB_NOSOUND | RecycleFlag.SHERB_NOCONFIRMATION);
  48. }
  49. internal static void CleanTemporaries()
  50. {
  51. EmptyFolder(TempFolder);
  52. }
  53. internal static void CleanFileZilla()
  54. {
  55. try
  56. {
  57. File.Delete(ProfileAppDataRoaming + "\\FileZilla\\recentservers.xml");
  58. }
  59. catch { }
  60. }
  61. internal static void CleanMiniDumps()
  62. {
  63. EmptyFolder(OSDriveWindows + "\\Minidump");
  64. }
  65. internal static void CleanErrorReports()
  66. {
  67. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ReportArchive");
  68. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ReportQueue");
  69. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\Temp");
  70. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ERC");
  71. EmptyFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ReportArchive");
  72. EmptyFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ReportQueue");
  73. EmptyFolder(ProgramData + "\\Microsoft\\Windows\\WER\\Temp");
  74. EmptyFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ERC");
  75. }
  76. internal static void CleanMediaPlayersCache()
  77. {
  78. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Media Player");
  79. EmptyFolder(ProfileAppDataLocalLow + "\\Apple Computer\\QuickTime\\downloads");
  80. EmptyFolder(ProfileAppDataRoaming + "\\Macromedia");
  81. try
  82. {
  83. File.Delete(ProfileAppDataLocalLow + "\\Apple Computer\\QuickTime\\QTPlayerSession.xml");
  84. }
  85. catch { }
  86. }
  87. internal static void CleanLogs()
  88. {
  89. EmptyFolder(System32Folder + "\\LogFiles");
  90. EmptyFolder(OSDrive + "\\inetpub\\logs\\LogFiles");
  91. }
  92. internal static ByteSize CheckFootprint()
  93. {
  94. try
  95. {
  96. DirectoryInfo info = new DirectoryInfo(TempFolder);
  97. return ByteSize.FromBytes(info.EnumerateFiles("*", SearchOption.AllDirectories).Sum(file => file.Length));
  98. }
  99. catch (Exception ex)
  100. {
  101. ErrorLogger.LogError("CleanHelper.CleanLogs", ex.Message, ex.StackTrace);
  102. return ByteSize.FromBytes(0);
  103. }
  104. }
  105. }
  106. }