Utilities.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Security.Principal;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.Diagnostics;
  10. using System.ServiceProcess;
  11. using System.Management.Automation;
  12. using System.Drawing;
  13. namespace Optimizer
  14. {
  15. internal static class Utilities
  16. {
  17. internal static readonly string LocalMachineRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
  18. internal static readonly string LocalMachineRunOnce = "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
  19. internal static readonly string LocalMachineRunWoW = "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run";
  20. internal static readonly string LocalMachineRunOnceWow = "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
  21. internal static readonly string CurrentUserRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
  22. internal static readonly string CurrentUserRunOnce = "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
  23. internal static readonly string LocalMachineStartupFolder = CleanHelper.ProgramData + "\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
  24. internal static readonly string CurrentUserStartupFolder = CleanHelper.ProfileAppDataRoaming + "\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
  25. internal readonly static string DefaultEdgeDownloadFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads");
  26. internal static WindowsVersion CurrentWindowsVersion = WindowsVersion.Unsupported;
  27. internal delegate void SetControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue);
  28. internal static void SetControlPropertyThreadSafe(Control control, string propertyName, object propertyValue)
  29. {
  30. if (control.InvokeRequired)
  31. {
  32. control.Invoke(new SetControlPropertyThreadSafeDelegate(SetControlPropertyThreadSafe), new object[] { control, propertyName, propertyValue });
  33. }
  34. else
  35. {
  36. control.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, control, new object[] { propertyValue });
  37. }
  38. }
  39. internal static IEnumerable<Control> GetSelfAndChildrenRecursive(Control parent)
  40. {
  41. List<Control> controls = new List<Control>();
  42. foreach (Control child in parent.Controls)
  43. {
  44. controls.AddRange(GetSelfAndChildrenRecursive(child));
  45. }
  46. controls.Add(parent);
  47. return controls;
  48. }
  49. internal static Color ToGrayScale(this Color originalColor)
  50. {
  51. if (originalColor.Equals(Color.Transparent))
  52. return originalColor;
  53. int grayScale = (int)((originalColor.R * .299) + (originalColor.G * .587) + (originalColor.B * .114));
  54. return Color.FromArgb(grayScale, grayScale, grayScale);
  55. }
  56. internal static string GetOS()
  57. {
  58. string os = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ProductName", "");
  59. if (os.Contains("Windows 7"))
  60. {
  61. CurrentWindowsVersion = WindowsVersion.Windows7;
  62. }
  63. if ((os.Contains("Windows 8")) || (os.Contains("Windows 8.1")))
  64. {
  65. CurrentWindowsVersion = WindowsVersion.Windows8;
  66. }
  67. if (os.Contains("Windows 10"))
  68. {
  69. CurrentWindowsVersion = WindowsVersion.Windows10;
  70. }
  71. return os;
  72. }
  73. internal static string GetBitness()
  74. {
  75. string bitness = string.Empty;
  76. if (Environment.Is64BitOperatingSystem)
  77. {
  78. bitness = "You are working with 64-bit architecture";
  79. }
  80. else
  81. {
  82. bitness = "You are working with 32-bit architecture";
  83. }
  84. return bitness;
  85. }
  86. internal static bool IsAdmin()
  87. {
  88. return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
  89. }
  90. internal static bool IsCompatible()
  91. {
  92. bool legit;
  93. string os = GetOS();
  94. if ((os.Contains("XP")) || (os.Contains("Vista")) || os.Contains("Server 2003"))
  95. {
  96. legit = false;
  97. }
  98. else
  99. {
  100. legit = true;
  101. }
  102. return legit;
  103. }
  104. internal static string GetEdgeDownloadFolder()
  105. {
  106. string current = string.Empty;
  107. try
  108. {
  109. current = Registry.GetValue(@"HKEY_CURRENT_USER\SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppContainer\Storage\microsoft.microsoftedge_8wekyb3d8bbwe\MicrosoftEdge\Main", "Default Download Directory", DefaultEdgeDownloadFolder).ToString();
  110. }
  111. catch
  112. {
  113. current = DefaultEdgeDownloadFolder;
  114. }
  115. return current;
  116. }
  117. internal static void SetEdgeDownloadFolder(string path)
  118. {
  119. Registry.SetValue(@"HKEY_CURRENT_USER\SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppContainer\Storage\microsoft.microsoftedge_8wekyb3d8bbwe\MicrosoftEdge\Main", "Default Download Directory", path, RegistryValueKind.String);
  120. }
  121. internal static void RunBatchFile(string batchFile)
  122. {
  123. try
  124. {
  125. using (Process p = new Process())
  126. {
  127. p.StartInfo.CreateNoWindow = true;
  128. p.StartInfo.FileName = batchFile;
  129. p.StartInfo.UseShellExecute = false;
  130. p.Start();
  131. p.WaitForExit();
  132. p.Close();
  133. }
  134. }
  135. catch { }
  136. }
  137. internal static void ImportRegistryScript(string scriptFile)
  138. {
  139. string path = "\"" + scriptFile + "\"";
  140. Process p = new Process();
  141. try
  142. {
  143. p.StartInfo.FileName = "regedit.exe";
  144. p.StartInfo.UseShellExecute = false;
  145. p = Process.Start("regedit.exe", "/s " + path);
  146. p.WaitForExit();
  147. }
  148. catch
  149. {
  150. p.Dispose();
  151. }
  152. finally
  153. {
  154. p.Dispose();
  155. }
  156. }
  157. internal static void Reboot()
  158. {
  159. Process.Start("shutdown", "/r /t 0");
  160. }
  161. internal static void ActivateMainForm()
  162. {
  163. Program.MainForm.Activate();
  164. }
  165. internal static bool ServiceExists(string serviceName)
  166. {
  167. return ServiceController.GetServices().Any(serviceController => serviceController.ServiceName.Equals(serviceName));
  168. }
  169. internal static void StopService(string serviceName)
  170. {
  171. if (ServiceExists(serviceName))
  172. {
  173. ServiceController sc = new ServiceController(serviceName);
  174. if (sc.CanStop)
  175. {
  176. sc.Stop();
  177. }
  178. }
  179. }
  180. internal static void StartService(string serviceName)
  181. {
  182. if (ServiceExists(serviceName))
  183. {
  184. ServiceController sc = new ServiceController(serviceName);
  185. try
  186. {
  187. sc.Start();
  188. }
  189. catch { }
  190. }
  191. }
  192. private static void GetRegistryStartupItemsHelper(ref List<StartupItem> list, StartupItemLocation location, StartupItemType type)
  193. {
  194. string keyPath = string.Empty;
  195. RegistryKey hive = null;
  196. if (location == StartupItemLocation.HKLM)
  197. {
  198. hive = Registry.LocalMachine;
  199. if (type == StartupItemType.Run)
  200. {
  201. keyPath = LocalMachineRun;
  202. }
  203. else if (type == StartupItemType.RunOnce)
  204. {
  205. keyPath = LocalMachineRunOnce;
  206. }
  207. }
  208. else if (location == StartupItemLocation.HKLMWoW)
  209. {
  210. hive = Registry.LocalMachine;
  211. if (type == StartupItemType.Run)
  212. {
  213. keyPath = LocalMachineRunWoW;
  214. }
  215. else if (type == StartupItemType.RunOnce)
  216. {
  217. keyPath = LocalMachineRunOnceWow;
  218. }
  219. }
  220. else if (location == StartupItemLocation.HKCU)
  221. {
  222. hive = Registry.CurrentUser;
  223. if (type == StartupItemType.Run)
  224. {
  225. keyPath = CurrentUserRun;
  226. }
  227. else if (type == StartupItemType.RunOnce)
  228. {
  229. keyPath = CurrentUserRunOnce;
  230. }
  231. }
  232. if (hive != null)
  233. {
  234. RegistryKey key = hive.OpenSubKey(keyPath, true);
  235. if (key != null)
  236. {
  237. string[] valueNames = key.GetValueNames();
  238. foreach (string x in valueNames)
  239. {
  240. try
  241. {
  242. RegistryStartupItem item = new RegistryStartupItem();
  243. item.Name = x;
  244. item.FileLocation = key.GetValue(x).ToString();
  245. item.Key = key;
  246. item.RegistryLocation = location;
  247. item.StartupType = type;
  248. list.Add(item);
  249. }
  250. catch { }
  251. }
  252. }
  253. }
  254. }
  255. private static void GetFolderStartupItemsHelper(ref List<StartupItem> list, string[] files, string[] shortcuts)
  256. {
  257. foreach (string file in files)
  258. {
  259. try
  260. {
  261. FolderStartupItem item = new FolderStartupItem();
  262. item.Name = Path.GetFileNameWithoutExtension(file);
  263. item.FileLocation = file;
  264. item.Shortcut = file;
  265. item.RegistryLocation = StartupItemLocation.Folder;
  266. list.Add(item);
  267. }
  268. catch { }
  269. }
  270. foreach (string shortcut in shortcuts)
  271. {
  272. try
  273. {
  274. FolderStartupItem item = new FolderStartupItem();
  275. item.Name = Path.GetFileNameWithoutExtension(shortcut);
  276. item.FileLocation = GetShortcutTargetFile(shortcut);
  277. item.Shortcut = shortcut;
  278. item.RegistryLocation = StartupItemLocation.Folder;
  279. list.Add(item);
  280. }
  281. catch { }
  282. }
  283. }
  284. internal static List<StartupItem> GetStartupItems()
  285. {
  286. List<StartupItem> startupItems = new List<StartupItem>();
  287. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLM, StartupItemType.Run);
  288. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLM, StartupItemType.RunOnce);
  289. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKCU, StartupItemType.Run);
  290. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKCU, StartupItemType.RunOnce);
  291. if (Environment.Is64BitOperatingSystem)
  292. {
  293. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLMWoW, StartupItemType.Run);
  294. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLMWoW, StartupItemType.RunOnce);
  295. }
  296. string[] currentUserFiles = Directory.GetFiles(CurrentUserStartupFolder, "*.exe", SearchOption.AllDirectories);
  297. string[] currentUserShortcuts = Directory.GetFiles(CurrentUserStartupFolder, "*.lnk", SearchOption.AllDirectories);
  298. GetFolderStartupItemsHelper(ref startupItems, currentUserFiles, currentUserShortcuts);
  299. string[] localMachineFiles = Directory.GetFiles(LocalMachineStartupFolder, "*.exe", SearchOption.AllDirectories);
  300. string[] localMachineShortcuts = Directory.GetFiles(LocalMachineStartupFolder, "*.lnk", SearchOption.AllDirectories);
  301. GetFolderStartupItemsHelper(ref startupItems, localMachineFiles, localMachineShortcuts);
  302. return startupItems;
  303. }
  304. internal static void EnableFirewall()
  305. {
  306. RunCommand("netsh advfirewall set currentprofile state on");
  307. }
  308. internal static void EnableCommandPrompt()
  309. {
  310. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Policies\\Microsoft\\Windows\\System"))
  311. {
  312. key.SetValue("DisableCMD", 0, RegistryValueKind.DWord);
  313. }
  314. }
  315. internal static void EnableControlPanel()
  316. {
  317. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  318. {
  319. key.SetValue("NoControlPanel", 0, RegistryValueKind.DWord);
  320. }
  321. }
  322. internal static void EnableFolderOptions()
  323. {
  324. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  325. {
  326. key.SetValue("NoFolderOptions", 0, RegistryValueKind.DWord);
  327. }
  328. }
  329. internal static void EnableRunDialog()
  330. {
  331. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  332. {
  333. key.SetValue("NoRun", 0, RegistryValueKind.DWord);
  334. }
  335. }
  336. internal static void EnableContextMenu()
  337. {
  338. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  339. {
  340. key.SetValue("NoViewContextMenu", 0, RegistryValueKind.DWord);
  341. }
  342. }
  343. internal static void EnableTaskManager()
  344. {
  345. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"))
  346. {
  347. key.SetValue("DisableTaskMgr", 0, RegistryValueKind.DWord);
  348. }
  349. }
  350. internal static void EnableRegistryEditor()
  351. {
  352. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"))
  353. {
  354. key.SetValue("DisableRegistryTools", 0, RegistryValueKind.DWord);
  355. }
  356. }
  357. internal static void RunCommand(string command)
  358. {
  359. using (Process p = new Process())
  360. {
  361. p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  362. p.StartInfo.FileName = "cmd.exe";
  363. p.StartInfo.Arguments = "/C " + command;
  364. try
  365. {
  366. p.Start();
  367. p.WaitForExit();
  368. p.Close();
  369. }
  370. catch { }
  371. }
  372. }
  373. internal static void FindFile(string fileName)
  374. {
  375. if (File.Exists(fileName))
  376. {
  377. Process.Start("explorer.exe", "/select, " + fileName);
  378. }
  379. }
  380. internal static string GetShortcutTargetFile(string shortcutFilename)
  381. {
  382. string pathOnly = Path.GetDirectoryName(shortcutFilename);
  383. string filenameOnly = Path.GetFileName(shortcutFilename);
  384. Shell32.Shell shell = new Shell32.Shell();
  385. Shell32.Folder folder = shell.NameSpace(pathOnly);
  386. Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
  387. if (folderItem != null)
  388. {
  389. Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
  390. return link.Path;
  391. }
  392. return string.Empty;
  393. }
  394. internal static void RestartExplorer()
  395. {
  396. const string explorer = "explorer.exe";
  397. string explorerPath = string.Format("{0}\\{1}", Environment.GetEnvironmentVariable("WINDIR"), explorer);
  398. foreach (Process process in Process.GetProcesses())
  399. {
  400. try
  401. {
  402. if (string.Compare(process.MainModule.FileName, explorerPath, StringComparison.OrdinalIgnoreCase) == 0)
  403. {
  404. process.Kill();
  405. }
  406. }
  407. catch { }
  408. }
  409. Process.Start(explorer);
  410. }
  411. internal static void FindKeyInRegistry(string key)
  412. {
  413. try
  414. {
  415. Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit", "LastKey", key);
  416. Process.Start("regedit");
  417. }
  418. catch { }
  419. }
  420. internal static List<string> GetModernApps()
  421. {
  422. List<string> modernApps = new List<string>();
  423. using (PowerShell script = PowerShell.Create())
  424. {
  425. script.AddScript("Get-AppxPackage | Select Name | Out-String -Stream");
  426. string tmp = string.Empty;
  427. foreach (PSObject x in script.Invoke())
  428. {
  429. tmp = x.ToString().Trim();
  430. if (!string.IsNullOrEmpty(tmp) && !tmp.Contains("---"))
  431. {
  432. if (tmp != "Name") modernApps.Add(tmp);
  433. }
  434. }
  435. }
  436. return modernApps;
  437. }
  438. internal static bool UninstallModernApp(string appName)
  439. {
  440. using (PowerShell script = PowerShell.Create())
  441. {
  442. script.AddScript(string.Format("Get-AppxPackage *{0}* | Remove-AppxPackage", appName));
  443. script.Invoke();
  444. return script.HadErrors;
  445. }
  446. }
  447. internal static void ResetConfiguration()
  448. {
  449. try
  450. {
  451. Directory.Delete(Required.CoreFolder, true);
  452. }
  453. catch { }
  454. finally
  455. {
  456. Application.Restart();
  457. }
  458. }
  459. }
  460. }