CleanHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. namespace Optimizer
  5. {
  6. internal static class CleanHelper
  7. {
  8. [DllImport("Shell32.dll")]
  9. static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlag dwFlags);
  10. internal static readonly string System32Folder = Environment.GetFolderPath(Environment.SpecialFolder.System);
  11. internal static readonly string TempFolder = Path.GetTempPath();
  12. internal static readonly string ProfileAppDataRoaming = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  13. internal static readonly string ProgramData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
  14. internal static readonly string ProfileAppDataLocal = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
  15. internal static readonly string ProfileAppDataLocalLow = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "Low";
  16. internal static readonly string OSDrive = System32Folder.Substring(0, 3);
  17. internal static readonly string OSDriveWindows = Environment.GetEnvironmentVariable("WINDIR", EnvironmentVariableTarget.Machine);
  18. internal static readonly string UTorrentCache = ProfileAppDataRoaming + "\\uTorrent\\dlimagecache";
  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 CleanUTorrent()
  53. {
  54. EmptyFolder(UTorrentCache);
  55. }
  56. internal static void CleanFileZilla()
  57. {
  58. try
  59. {
  60. File.Delete(ProfileAppDataRoaming + "\\FileZilla\\recentservers.xml");
  61. }
  62. catch { }
  63. }
  64. internal static void CleanMiniDumps()
  65. {
  66. EmptyFolder(OSDriveWindows + "\\Minidump");
  67. }
  68. internal static void CleanErrorReports()
  69. {
  70. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ReportArchive");
  71. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ReportQueue");
  72. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\Temp");
  73. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ERC");
  74. EmptyFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ReportArchive");
  75. EmptyFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ReportQueue");
  76. EmptyFolder(ProgramData + "\\Microsoft\\Windows\\WER\\Temp");
  77. EmptyFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ERC");
  78. }
  79. internal static void CleanPrefetch()
  80. {
  81. EmptyFolder(OSDriveWindows + "\\Prefetch");
  82. }
  83. internal static void CleanMediaPlayersCache()
  84. {
  85. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Media Player");
  86. EmptyFolder(ProfileAppDataLocalLow + "\\Apple Computer\\QuickTime\\downloads");
  87. EmptyFolder(ProfileAppDataRoaming + "\\Macromedia");
  88. try
  89. {
  90. File.Delete(ProfileAppDataLocalLow + "\\Apple Computer\\QuickTime\\QTPlayerSession.xml");
  91. }
  92. catch { }
  93. }
  94. internal static void CleanLogs()
  95. {
  96. EmptyFolder(System32Folder + "\\LogFiles");
  97. EmptyFolder(OSDrive + "\\inetpub\\logs\\LogFiles");
  98. }
  99. }
  100. }