Utilities.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. if (Program.UNSAFE_MODE)
  72. {
  73. if (os.Contains("Windows Server 2008"))
  74. {
  75. CurrentWindowsVersion = WindowsVersion.Windows7;
  76. }
  77. if (os.Contains("Windows Server 2012"))
  78. {
  79. CurrentWindowsVersion = WindowsVersion.Windows8;
  80. }
  81. if (os.Contains("Windows Server 2016"))
  82. {
  83. CurrentWindowsVersion = WindowsVersion.Windows10;
  84. }
  85. }
  86. return os;
  87. }
  88. internal static string GetBitness()
  89. {
  90. string bitness = string.Empty;
  91. if (Environment.Is64BitOperatingSystem)
  92. {
  93. bitness = "You are working with 64-bit architecture";
  94. }
  95. else
  96. {
  97. bitness = "You are working with 32-bit architecture";
  98. }
  99. return bitness;
  100. }
  101. internal static bool IsAdmin()
  102. {
  103. return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
  104. }
  105. internal static bool IsCompatible()
  106. {
  107. bool legit;
  108. string os = GetOS();
  109. if ((os.Contains("XP")) || (os.Contains("Vista")) || os.Contains("Server 2003"))
  110. {
  111. legit = false;
  112. }
  113. else
  114. {
  115. legit = true;
  116. }
  117. return legit;
  118. }
  119. internal static string GetEdgeDownloadFolder()
  120. {
  121. string current = string.Empty;
  122. try
  123. {
  124. 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();
  125. }
  126. catch
  127. {
  128. current = DefaultEdgeDownloadFolder;
  129. }
  130. return current;
  131. }
  132. internal static void SetEdgeDownloadFolder(string path)
  133. {
  134. 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);
  135. }
  136. internal static void RunBatchFile(string batchFile)
  137. {
  138. try
  139. {
  140. using (Process p = new Process())
  141. {
  142. p.StartInfo.CreateNoWindow = true;
  143. p.StartInfo.FileName = batchFile;
  144. p.StartInfo.UseShellExecute = false;
  145. p.Start();
  146. p.WaitForExit();
  147. p.Close();
  148. }
  149. }
  150. catch { }
  151. }
  152. internal static void ImportRegistryScript(string scriptFile)
  153. {
  154. string path = "\"" + scriptFile + "\"";
  155. Process p = new Process();
  156. try
  157. {
  158. p.StartInfo.FileName = "regedit.exe";
  159. p.StartInfo.UseShellExecute = false;
  160. p = Process.Start("regedit.exe", "/s " + path);
  161. p.WaitForExit();
  162. }
  163. catch
  164. {
  165. p.Dispose();
  166. }
  167. finally
  168. {
  169. p.Dispose();
  170. }
  171. }
  172. internal static void Reboot()
  173. {
  174. Process.Start("shutdown", "/r /t 0");
  175. }
  176. internal static void ActivateMainForm()
  177. {
  178. Program.MainForm.Activate();
  179. }
  180. internal static bool ServiceExists(string serviceName)
  181. {
  182. return ServiceController.GetServices().Any(serviceController => serviceController.ServiceName.Equals(serviceName));
  183. }
  184. internal static void StopService(string serviceName)
  185. {
  186. if (ServiceExists(serviceName))
  187. {
  188. ServiceController sc = new ServiceController(serviceName);
  189. if (sc.CanStop)
  190. {
  191. sc.Stop();
  192. }
  193. }
  194. }
  195. internal static void StartService(string serviceName)
  196. {
  197. if (ServiceExists(serviceName))
  198. {
  199. ServiceController sc = new ServiceController(serviceName);
  200. try
  201. {
  202. sc.Start();
  203. }
  204. catch { }
  205. }
  206. }
  207. private static void GetRegistryStartupItemsHelper(ref List<StartupItem> list, StartupItemLocation location, StartupItemType type)
  208. {
  209. string keyPath = string.Empty;
  210. RegistryKey hive = null;
  211. if (location == StartupItemLocation.HKLM)
  212. {
  213. hive = Registry.LocalMachine;
  214. if (type == StartupItemType.Run)
  215. {
  216. keyPath = LocalMachineRun;
  217. }
  218. else if (type == StartupItemType.RunOnce)
  219. {
  220. keyPath = LocalMachineRunOnce;
  221. }
  222. }
  223. else if (location == StartupItemLocation.HKLMWoW)
  224. {
  225. hive = Registry.LocalMachine;
  226. if (type == StartupItemType.Run)
  227. {
  228. keyPath = LocalMachineRunWoW;
  229. }
  230. else if (type == StartupItemType.RunOnce)
  231. {
  232. keyPath = LocalMachineRunOnceWow;
  233. }
  234. }
  235. else if (location == StartupItemLocation.HKCU)
  236. {
  237. hive = Registry.CurrentUser;
  238. if (type == StartupItemType.Run)
  239. {
  240. keyPath = CurrentUserRun;
  241. }
  242. else if (type == StartupItemType.RunOnce)
  243. {
  244. keyPath = CurrentUserRunOnce;
  245. }
  246. }
  247. if (hive != null)
  248. {
  249. RegistryKey key = hive.OpenSubKey(keyPath, true);
  250. if (key != null)
  251. {
  252. string[] valueNames = key.GetValueNames();
  253. foreach (string x in valueNames)
  254. {
  255. try
  256. {
  257. RegistryStartupItem item = new RegistryStartupItem();
  258. item.Name = x;
  259. item.FileLocation = key.GetValue(x).ToString();
  260. item.Key = key;
  261. item.RegistryLocation = location;
  262. item.StartupType = type;
  263. list.Add(item);
  264. }
  265. catch { }
  266. }
  267. }
  268. }
  269. }
  270. private static void GetFolderStartupItemsHelper(ref List<StartupItem> list, string[] files, string[] shortcuts)
  271. {
  272. foreach (string file in files)
  273. {
  274. try
  275. {
  276. FolderStartupItem item = new FolderStartupItem();
  277. item.Name = Path.GetFileNameWithoutExtension(file);
  278. item.FileLocation = file;
  279. item.Shortcut = file;
  280. item.RegistryLocation = StartupItemLocation.Folder;
  281. list.Add(item);
  282. }
  283. catch { }
  284. }
  285. foreach (string shortcut in shortcuts)
  286. {
  287. try
  288. {
  289. FolderStartupItem item = new FolderStartupItem();
  290. item.Name = Path.GetFileNameWithoutExtension(shortcut);
  291. item.FileLocation = GetShortcutTargetFile(shortcut);
  292. item.Shortcut = shortcut;
  293. item.RegistryLocation = StartupItemLocation.Folder;
  294. list.Add(item);
  295. }
  296. catch { }
  297. }
  298. }
  299. internal static List<StartupItem> GetStartupItems()
  300. {
  301. List<StartupItem> startupItems = new List<StartupItem>();
  302. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLM, StartupItemType.Run);
  303. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLM, StartupItemType.RunOnce);
  304. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKCU, StartupItemType.Run);
  305. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKCU, StartupItemType.RunOnce);
  306. if (Environment.Is64BitOperatingSystem)
  307. {
  308. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLMWoW, StartupItemType.Run);
  309. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLMWoW, StartupItemType.RunOnce);
  310. }
  311. string[] currentUserFiles = Directory.GetFiles(CurrentUserStartupFolder, "*.exe", SearchOption.AllDirectories);
  312. string[] currentUserShortcuts = Directory.GetFiles(CurrentUserStartupFolder, "*.lnk", SearchOption.AllDirectories);
  313. GetFolderStartupItemsHelper(ref startupItems, currentUserFiles, currentUserShortcuts);
  314. string[] localMachineFiles = Directory.GetFiles(LocalMachineStartupFolder, "*.exe", SearchOption.AllDirectories);
  315. string[] localMachineShortcuts = Directory.GetFiles(LocalMachineStartupFolder, "*.lnk", SearchOption.AllDirectories);
  316. GetFolderStartupItemsHelper(ref startupItems, localMachineFiles, localMachineShortcuts);
  317. return startupItems;
  318. }
  319. internal static void EnableFirewall()
  320. {
  321. RunCommand("netsh advfirewall set currentprofile state on");
  322. }
  323. internal static void EnableCommandPrompt()
  324. {
  325. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Policies\\Microsoft\\Windows\\System"))
  326. {
  327. key.SetValue("DisableCMD", 0, RegistryValueKind.DWord);
  328. }
  329. }
  330. internal static void EnableControlPanel()
  331. {
  332. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  333. {
  334. key.SetValue("NoControlPanel", 0, RegistryValueKind.DWord);
  335. }
  336. }
  337. internal static void EnableFolderOptions()
  338. {
  339. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  340. {
  341. key.SetValue("NoFolderOptions", 0, RegistryValueKind.DWord);
  342. }
  343. }
  344. internal static void EnableRunDialog()
  345. {
  346. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  347. {
  348. key.SetValue("NoRun", 0, RegistryValueKind.DWord);
  349. }
  350. }
  351. internal static void EnableContextMenu()
  352. {
  353. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  354. {
  355. key.SetValue("NoViewContextMenu", 0, RegistryValueKind.DWord);
  356. }
  357. }
  358. internal static void EnableTaskManager()
  359. {
  360. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"))
  361. {
  362. key.SetValue("DisableTaskMgr", 0, RegistryValueKind.DWord);
  363. }
  364. }
  365. internal static void EnableRegistryEditor()
  366. {
  367. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"))
  368. {
  369. key.SetValue("DisableRegistryTools", 0, RegistryValueKind.DWord);
  370. }
  371. }
  372. internal static void RunCommand(string command)
  373. {
  374. using (Process p = new Process())
  375. {
  376. p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  377. p.StartInfo.FileName = "cmd.exe";
  378. p.StartInfo.Arguments = "/C " + command;
  379. try
  380. {
  381. p.Start();
  382. p.WaitForExit();
  383. p.Close();
  384. }
  385. catch { }
  386. }
  387. }
  388. internal static void FindFile(string fileName)
  389. {
  390. if (File.Exists(fileName))
  391. {
  392. Process.Start("explorer.exe", "/select, " + fileName);
  393. }
  394. }
  395. internal static string GetShortcutTargetFile(string shortcutFilename)
  396. {
  397. string pathOnly = Path.GetDirectoryName(shortcutFilename);
  398. string filenameOnly = Path.GetFileName(shortcutFilename);
  399. Shell32.Shell shell = new Shell32.Shell();
  400. Shell32.Folder folder = shell.NameSpace(pathOnly);
  401. Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
  402. if (folderItem != null)
  403. {
  404. Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
  405. return link.Path;
  406. }
  407. return string.Empty;
  408. }
  409. internal static void RestartExplorer()
  410. {
  411. const string explorer = "explorer.exe";
  412. string explorerPath = string.Format("{0}\\{1}", Environment.GetEnvironmentVariable("WINDIR"), explorer);
  413. foreach (Process process in Process.GetProcesses())
  414. {
  415. try
  416. {
  417. if (string.Compare(process.MainModule.FileName, explorerPath, StringComparison.OrdinalIgnoreCase) == 0)
  418. {
  419. process.Kill();
  420. }
  421. }
  422. catch { }
  423. }
  424. Process.Start(explorer);
  425. }
  426. internal static void FindKeyInRegistry(string key)
  427. {
  428. try
  429. {
  430. Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit", "LastKey", key);
  431. Process.Start("regedit");
  432. }
  433. catch { }
  434. }
  435. internal static List<string> GetModernApps()
  436. {
  437. List<string> modernApps = new List<string>();
  438. using (PowerShell script = PowerShell.Create())
  439. {
  440. script.AddScript("Get-AppxPackage | Select Name | Out-String -Stream");
  441. string tmp = string.Empty;
  442. foreach (PSObject x in script.Invoke())
  443. {
  444. tmp = x.ToString().Trim();
  445. if (!string.IsNullOrEmpty(tmp) && !tmp.Contains("---"))
  446. {
  447. if (tmp != "Name") modernApps.Add(tmp);
  448. }
  449. }
  450. }
  451. return modernApps;
  452. }
  453. internal static bool UninstallModernApp(string appName)
  454. {
  455. using (PowerShell script = PowerShell.Create())
  456. {
  457. script.AddScript(string.Format("Get-AppxPackage *{0}* | Remove-AppxPackage", appName));
  458. script.Invoke();
  459. return script.HadErrors;
  460. }
  461. }
  462. internal static void ResetConfiguration()
  463. {
  464. try
  465. {
  466. Directory.Delete(Required.CoreFolder, true);
  467. }
  468. catch { }
  469. finally
  470. {
  471. Application.Restart();
  472. }
  473. }
  474. }
  475. }