CleanHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.IO;
  8. using System.Runtime.InteropServices;
  9. using Microsoft.Win32;
  10. using System.Text.RegularExpressions;
  11. using System.Windows.Forms;
  12. using System.Diagnostics;
  13. namespace Optimizer
  14. {
  15. internal static class CleanHelper
  16. {
  17. [DllImport("Shell32.dll")]
  18. static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlag dwFlags);
  19. internal static readonly string System32Folder = Environment.GetFolderPath(Environment.SpecialFolder.System);
  20. internal static readonly string TempFolder = Path.GetTempPath();
  21. internal static readonly string ProfileAppDataRoaming = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  22. internal static readonly string ProgramData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
  23. internal static readonly string ProfileAppDataLocal = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
  24. internal static readonly string ProfileAppDataLocalLow = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "Low";
  25. internal static readonly string OSDrive = System32Folder.Substring(0, 3);
  26. internal static readonly string OSDriveWindows = Environment.GetEnvironmentVariable("WINDIR", EnvironmentVariableTarget.Machine);
  27. internal static readonly string UTorrentCache = ProfileAppDataRoaming + "\\uTorrent\\dlimagecache";
  28. internal static void EmptyFolder(string path)
  29. {
  30. try
  31. {
  32. DirectoryInfo di = new DirectoryInfo(path);
  33. foreach (FileInfo file in di.GetFiles())
  34. {
  35. try
  36. {
  37. file.IsReadOnly = false;
  38. file.Delete();
  39. }
  40. catch { }
  41. }
  42. foreach (DirectoryInfo dir in di.GetDirectories())
  43. {
  44. try
  45. {
  46. dir.Delete(true);
  47. }
  48. catch { }
  49. }
  50. }
  51. catch { }
  52. }
  53. internal static void EmptyRecycleBin()
  54. {
  55. SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlag.SHERB_NOSOUND | RecycleFlag.SHERB_NOCONFIRMATION);
  56. }
  57. internal static void CleanTemporaries()
  58. {
  59. EmptyFolder(TempFolder);
  60. }
  61. internal static void CleanUTorrent()
  62. {
  63. EmptyFolder(UTorrentCache);
  64. }
  65. internal static void CleanFileZilla()
  66. {
  67. try
  68. {
  69. File.Delete(ProfileAppDataRoaming + "\\FileZilla\\recentservers.xml");
  70. }
  71. catch { }
  72. }
  73. internal static void CleanMiniDumps()
  74. {
  75. EmptyFolder(OSDriveWindows + "\\Minidump");
  76. }
  77. internal static void CleanErrorReports()
  78. {
  79. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ReportArchive");
  80. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ReportQueue");
  81. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\Temp");
  82. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ERC");
  83. EmptyFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ReportArchive");
  84. EmptyFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ReportQueue");
  85. EmptyFolder(ProgramData + "\\Microsoft\\Windows\\WER\\Temp");
  86. EmptyFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ERC");
  87. }
  88. internal static void CleanPrefetch()
  89. {
  90. EmptyFolder(OSDriveWindows + "\\Prefetch");
  91. }
  92. internal static void CleanMediaPlayersCache()
  93. {
  94. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Media Player");
  95. EmptyFolder(ProfileAppDataLocalLow + "\\Apple Computer\\QuickTime\\downloads");
  96. EmptyFolder(ProfileAppDataRoaming + "\\Macromedia");
  97. try
  98. {
  99. File.Delete(ProfileAppDataLocalLow + "\\Apple Computer\\QuickTime\\QTPlayerSession.xml");
  100. }
  101. catch { }
  102. }
  103. internal static void CleanLogs()
  104. {
  105. EmptyFolder(System32Folder + "\\LogFiles");
  106. EmptyFolder(OSDrive + "\\inetpub\\logs\\LogFiles");
  107. }
  108. }
  109. }