CleanHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. public 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 readonly string LocalMachineRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
  29. internal static readonly string LocalMachineRunOnce = "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
  30. internal static readonly string LocalMachineRunWoW = "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run";
  31. internal static readonly string LocalMachineRunOnceWow = "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
  32. internal static readonly string CurrentUserRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
  33. internal static readonly string CurrentUserRunOnce = "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
  34. internal static readonly string LocalMachineStartupFolder = ProgramData + "\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
  35. internal static readonly string CurrentUserStartupFolder = ProfileAppDataRoaming + "\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
  36. internal static void EmptyFolder(string path)
  37. {
  38. try
  39. {
  40. DirectoryInfo di = new DirectoryInfo(path);
  41. foreach (FileInfo file in di.GetFiles())
  42. {
  43. try
  44. {
  45. file.IsReadOnly = false;
  46. file.Delete();
  47. }
  48. catch { }
  49. }
  50. foreach (DirectoryInfo dir in di.GetDirectories())
  51. {
  52. try
  53. {
  54. dir.Delete(true);
  55. }
  56. catch { }
  57. }
  58. }
  59. catch { }
  60. }
  61. internal static void EmptyRecycleBin()
  62. {
  63. SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlag.SHERB_NOSOUND | RecycleFlag.SHERB_NOCONFIRMATION);
  64. }
  65. internal static void CleanTemp()
  66. {
  67. EmptyFolder(TempFolder);
  68. }
  69. internal static void CleanUTorrent()
  70. {
  71. EmptyFolder(uTorrentCache);
  72. }
  73. internal static void CleanFileZilla()
  74. {
  75. try
  76. {
  77. File.Delete(ProfileAppDataRoaming + "\\FileZilla\\recentservers.xml");
  78. }
  79. catch (Exception error) { MessageBox.Show(error.Message); }
  80. }
  81. internal static void CleanMiniDumps()
  82. {
  83. EmptyFolder(OSDriveWindows + "\\Minidump");
  84. }
  85. internal static void CleanErrorReports()
  86. {
  87. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ReportArchive");
  88. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ReportQueue");
  89. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\Temp");
  90. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Windows\\WER\\ERC");
  91. EmptyFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ReportArchive");
  92. EmptyFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ReportQueue");
  93. EmptyFolder(ProgramData + "\\Microsoft\\Windows\\WER\\Temp");
  94. EmptyFolder(ProgramData + "\\Microsoft\\Windows\\WER\\ERC");
  95. }
  96. internal static void CleanPrefetch()
  97. {
  98. EmptyFolder(OSDriveWindows + "\\Prefetch");
  99. }
  100. internal static void CleanMediaPlayersCache()
  101. {
  102. EmptyFolder(ProfileAppDataLocal + "\\Microsoft\\Media Player");
  103. EmptyFolder(ProfileAppDataLocalLow + "\\Apple Computer\\QuickTime\\downloads");
  104. EmptyFolder(ProfileAppDataRoaming + "\\Macromedia");
  105. try
  106. {
  107. File.Delete(ProfileAppDataLocalLow + "\\Apple Computer\\QuickTime\\QTPlayerSession.xml");
  108. }
  109. catch { }
  110. }
  111. internal static void CleanLogs()
  112. {
  113. EmptyFolder(System32Folder + "\\LogFiles");
  114. EmptyFolder(OSDrive + "\\inetpub\\logs\\LogFiles");
  115. }
  116. internal static List<StartupItem> GetStartupItems()
  117. {
  118. List<StartupItem> collection = new List<StartupItem>();
  119. RegistryKey registryKey = null;
  120. // Get Local Machine Run startup items
  121. try
  122. {
  123. registryKey = Registry.LocalMachine.OpenSubKey(LocalMachineRun, true);
  124. string[] valueNames = registryKey.GetValueNames();
  125. foreach (string s in valueNames)
  126. {
  127. RegistryStartupItem item = new RegistryStartupItem();
  128. item.Name = s;
  129. item.Location = registryKey.GetValue(s).ToString();
  130. item.Key = registryKey;
  131. item.RegistryLocation = StartupItemLocation.HKLM;
  132. item.RegistryType = StartupItemType.Run;
  133. collection.Add(item);
  134. }
  135. }
  136. catch { }
  137. // Get Local Machine Run Once startup items
  138. try
  139. {
  140. registryKey = Registry.LocalMachine.OpenSubKey(LocalMachineRunOnce, true);
  141. string[] valueNames = registryKey.GetValueNames();
  142. foreach (string s in valueNames)
  143. {
  144. RegistryStartupItem item = new RegistryStartupItem();
  145. item.Name = s;
  146. item.Location = registryKey.GetValue(s).ToString();
  147. item.Key = registryKey;
  148. item.RegistryLocation = StartupItemLocation.HKLM;
  149. item.RegistryType = StartupItemType.RunOnce;
  150. collection.Add(item);
  151. }
  152. }
  153. catch { }
  154. // Get Local Machine Run WoW startup items
  155. try
  156. {
  157. registryKey = Registry.LocalMachine.OpenSubKey(LocalMachineRunWoW, true);
  158. string[] valueNames2 = registryKey.GetValueNames();
  159. foreach (string s in valueNames2)
  160. {
  161. RegistryStartupItem item = new RegistryStartupItem();
  162. item.Name = s;
  163. item.Location = registryKey.GetValue(s).ToString();
  164. item.Key = registryKey;
  165. item.RegistryLocation = StartupItemLocation.HKLMWoW;
  166. item.RegistryType = StartupItemType.Run;
  167. collection.Add(item);
  168. }
  169. }
  170. catch { }
  171. // Get Local Machine Run Once WoW startup items
  172. try
  173. {
  174. registryKey = Registry.LocalMachine.OpenSubKey(LocalMachineRunOnceWow, true);
  175. string[] valueNames2 = registryKey.GetValueNames();
  176. foreach (string s in valueNames2)
  177. {
  178. RegistryStartupItem item = new RegistryStartupItem();
  179. item.Name = s;
  180. item.Location = registryKey.GetValue(s).ToString();
  181. item.Key = registryKey;
  182. item.RegistryLocation = StartupItemLocation.HKLMWoW;
  183. item.RegistryType = StartupItemType.RunOnce;
  184. collection.Add(item);
  185. }
  186. }
  187. catch { }
  188. // Get Current User Run startup items
  189. try
  190. {
  191. registryKey = Registry.CurrentUser.OpenSubKey(CurrentUserRun, true);
  192. string[] valueNames3 = registryKey.GetValueNames();
  193. foreach (string s in valueNames3)
  194. {
  195. RegistryStartupItem item = new RegistryStartupItem();
  196. item.Name = s;
  197. item.Location = registryKey.GetValue(s).ToString();
  198. item.Key = registryKey;
  199. item.RegistryLocation = StartupItemLocation.HKCU;
  200. item.RegistryType = StartupItemType.Run;
  201. collection.Add(item);
  202. }
  203. }
  204. catch { }
  205. // Get Current User Run Once startup items
  206. try
  207. {
  208. registryKey = Registry.CurrentUser.OpenSubKey(CurrentUserRunOnce, true);
  209. string[] valueNames3 = registryKey.GetValueNames();
  210. foreach (string s in valueNames3)
  211. {
  212. RegistryStartupItem item = new RegistryStartupItem();
  213. item.Name = s;
  214. item.Location = registryKey.GetValue(s).ToString();
  215. item.Key = registryKey;
  216. item.RegistryLocation = StartupItemLocation.HKCU;
  217. item.RegistryType = StartupItemType.RunOnce;
  218. collection.Add(item);
  219. }
  220. }
  221. catch { }
  222. // Get Current User Startup folder startup items
  223. try
  224. {
  225. // get shortcuts to files
  226. string[] shortcuts = Directory.GetFiles(CurrentUserStartupFolder, "*.lnk", SearchOption.AllDirectories);
  227. // get executables
  228. string[] files = Directory.GetFiles(CurrentUserStartupFolder, "*.exe", SearchOption.AllDirectories);
  229. foreach (string shortcut in shortcuts)
  230. {
  231. FolderStartupItem item = new FolderStartupItem();
  232. item.Name = Path.GetFileNameWithoutExtension(shortcut);
  233. item.Location = GetShortcutTargetFile(shortcut);
  234. item.Shortcut = shortcut;
  235. item.RegistryLocation = StartupItemLocation.Folder;
  236. collection.Add(item);
  237. }
  238. foreach (string file in files)
  239. {
  240. FolderStartupItem item2 = new FolderStartupItem();
  241. item2.Name = Path.GetFileNameWithoutExtension(file);
  242. item2.Location = file;
  243. item2.Shortcut = file;
  244. item2.RegistryLocation = StartupItemLocation.Folder;
  245. collection.Add(item2);
  246. }
  247. }
  248. catch { }
  249. // Get Local Machine Startup folder startup items
  250. try
  251. {
  252. // get shortcuts to files
  253. string[] shortcuts = Directory.GetFiles(LocalMachineStartupFolder, "*.lnk", SearchOption.AllDirectories);
  254. // get executables
  255. string[] files = Directory.GetFiles(LocalMachineStartupFolder, "*.exe", SearchOption.AllDirectories);
  256. foreach (string shortcut in shortcuts)
  257. {
  258. FolderStartupItem item = new FolderStartupItem();
  259. item.Name = Path.GetFileNameWithoutExtension(shortcut);
  260. item.Location = GetShortcutTargetFile(shortcut);
  261. item.Shortcut = shortcut;
  262. item.RegistryLocation = StartupItemLocation.Folder;
  263. collection.Add(item);
  264. }
  265. foreach (string file in files)
  266. {
  267. FolderStartupItem item2 = new FolderStartupItem();
  268. item2.Name = Path.GetFileNameWithoutExtension(file);
  269. item2.Location = file;
  270. item2.Shortcut = file;
  271. item2.RegistryLocation = StartupItemLocation.Folder;
  272. collection.Add(item2);
  273. }
  274. }
  275. catch { }
  276. return collection;
  277. }
  278. internal static void EnableFirewall()
  279. {
  280. RunCommand("netsh advfirewall set currentprofile state on");
  281. }
  282. internal static void EnableCommandPrompt()
  283. {
  284. RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Policies\\Microsoft\\Windows\\System");
  285. key.SetValue("DisableCMD", 0, RegistryValueKind.DWord);
  286. key.Close();
  287. }
  288. internal static void EnableControlPanel()
  289. {
  290. RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
  291. key.SetValue("NoControlPanel", 0, RegistryValueKind.DWord);
  292. key.Close();
  293. }
  294. internal static void EnableFolderOptions()
  295. {
  296. RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
  297. key.SetValue("NoFolderOptions", 0, RegistryValueKind.DWord);
  298. key.Close();
  299. }
  300. internal static void EnableRunDialog()
  301. {
  302. RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
  303. key.SetValue("NoRun", 0, RegistryValueKind.DWord);
  304. key.Close();
  305. }
  306. internal static void EnableContextMenu()
  307. {
  308. RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
  309. key.SetValue("NoViewContextMenu", 0, RegistryValueKind.DWord);
  310. key.Close();
  311. }
  312. internal static void EnableTaskManager()
  313. {
  314. RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
  315. key.SetValue("DisableTaskMgr", 0, RegistryValueKind.DWord);
  316. key.Close();
  317. }
  318. internal static void EnableRegistryEditor()
  319. {
  320. RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
  321. key.SetValue("DisableRegistryTools", 0, RegistryValueKind.DWord);
  322. key.Close();
  323. }
  324. internal static void RunCommand(string cmd)
  325. {
  326. new Process
  327. {
  328. StartInfo = new ProcessStartInfo
  329. {
  330. WindowStyle = ProcessWindowStyle.Hidden,
  331. FileName = "cmd.exe",
  332. Arguments = "/C" + cmd
  333. }
  334. }.Start();
  335. }
  336. internal static void FindFile(string filename)
  337. {
  338. if (File.Exists(filename))
  339. {
  340. Process.Start("explorer.exe", "/select, " + filename);
  341. }
  342. }
  343. internal static string GetShortcutTargetFile(string shortcutFilename)
  344. {
  345. string pathOnly = Path.GetDirectoryName(shortcutFilename);
  346. string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);
  347. Shell32.Shell shell = new Shell32.Shell();
  348. Shell32.Folder folder = shell.NameSpace(pathOnly);
  349. Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
  350. if (folderItem != null)
  351. {
  352. Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
  353. return link.Path;
  354. }
  355. return string.Empty; // not found
  356. }
  357. internal static void RestartExplorer()
  358. {
  359. const string explorer = "explorer.exe";
  360. string explorerPath = string.Format("{0}\\{1}", Environment.GetEnvironmentVariable("WINDIR"), explorer);
  361. foreach (Process process in Process.GetProcesses())
  362. {
  363. try
  364. {
  365. if (string.Compare(process.MainModule.FileName, explorerPath, StringComparison.OrdinalIgnoreCase) == 0)
  366. {
  367. process.Kill();
  368. }
  369. }
  370. catch { }
  371. }
  372. Process.Start(explorer);
  373. }
  374. internal static void FindKeyInRegistry(string key)
  375. {
  376. try
  377. {
  378. Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit", "LastKey", key);
  379. Process.Start("regedit");
  380. }
  381. catch { }
  382. }
  383. }
  384. }