Utilities.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Security.AccessControl;
  10. using System.Security.Principal;
  11. using System.ServiceProcess;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace Optimizer
  16. {
  17. internal static class Utilities
  18. {
  19. // DEPRECATED
  20. //internal readonly static string DefaultEdgeDownloadFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads");
  21. internal static WindowsVersion CurrentWindowsVersion = WindowsVersion.Unsupported;
  22. static string productName = string.Empty;
  23. static string buildNumber = string.Empty;
  24. internal delegate void SetControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue);
  25. internal static void SetControlPropertyThreadSafe(Control control, string propertyName, object propertyValue)
  26. {
  27. if (control.InvokeRequired)
  28. {
  29. control.Invoke(new SetControlPropertyThreadSafeDelegate(SetControlPropertyThreadSafe), new object[] { control, propertyName, propertyValue });
  30. }
  31. else
  32. {
  33. control.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, control, new object[] { propertyValue });
  34. }
  35. }
  36. internal static IEnumerable<Control> GetSelfAndChildrenRecursive(Control parent)
  37. {
  38. List<Control> controls = new List<Control>();
  39. foreach (Control child in parent.Controls)
  40. {
  41. controls.AddRange(GetSelfAndChildrenRecursive(child));
  42. }
  43. controls.Add(parent);
  44. return controls;
  45. }
  46. internal static Color ToGrayScale(this Color originalColor)
  47. {
  48. if (originalColor.Equals(Color.Transparent))
  49. return originalColor;
  50. int grayScale = (int)((originalColor.R * .299) + (originalColor.G * .587) + (originalColor.B * .114));
  51. return Color.FromArgb(grayScale, grayScale, grayScale);
  52. }
  53. internal static string GetWindows10Build()
  54. {
  55. return (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ReleaseId", "");
  56. }
  57. internal static string GetOS()
  58. {
  59. productName = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ProductName", "");
  60. if (productName.Contains("Windows 7"))
  61. {
  62. CurrentWindowsVersion = WindowsVersion.Windows7;
  63. }
  64. if ((productName.Contains("Windows 8")) || (productName.Contains("Windows 8.1")))
  65. {
  66. CurrentWindowsVersion = WindowsVersion.Windows8;
  67. }
  68. if (productName.Contains("Windows 10"))
  69. {
  70. buildNumber = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "CurrentBuild", "");
  71. if (Convert.ToInt32(buildNumber) >= 22000)
  72. {
  73. productName = productName.Replace("Windows 10", "Windows 11");
  74. CurrentWindowsVersion = WindowsVersion.Windows11;
  75. }
  76. else
  77. {
  78. CurrentWindowsVersion = WindowsVersion.Windows10;
  79. }
  80. }
  81. if (Program.UNSAFE_MODE)
  82. {
  83. if (productName.Contains("Windows Server 2008"))
  84. {
  85. CurrentWindowsVersion = WindowsVersion.Windows7;
  86. }
  87. if (productName.Contains("Windows Server 2012"))
  88. {
  89. CurrentWindowsVersion = WindowsVersion.Windows8;
  90. }
  91. if (productName.Contains("Windows Server 2016") || productName.Contains("Windows Server 2019") || productName.Contains("Windows Server 2022"))
  92. {
  93. CurrentWindowsVersion = WindowsVersion.Windows10;
  94. }
  95. }
  96. return productName;
  97. }
  98. internal static string GetBitness()
  99. {
  100. string bitness = string.Empty;
  101. if (Environment.Is64BitOperatingSystem)
  102. {
  103. bitness = "You are working with 64-bit";
  104. }
  105. else
  106. {
  107. bitness = "You are working with 32-bit";
  108. }
  109. return bitness;
  110. }
  111. internal static bool IsAdmin()
  112. {
  113. return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
  114. }
  115. internal static bool IsCompatible()
  116. {
  117. bool legit;
  118. string os = GetOS();
  119. if ((os.Contains("XP")) || (os.Contains("Vista")) || os.Contains("Server 2003"))
  120. {
  121. legit = false;
  122. }
  123. else
  124. {
  125. legit = true;
  126. }
  127. return legit;
  128. }
  129. // DEPRECATED
  130. //internal static string GetEdgeDownloadFolder()
  131. //{
  132. // string current = string.Empty;
  133. // try
  134. // {
  135. // current = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge", "DownloadDirectory", DefaultEdgeDownloadFolder).ToString();
  136. // }
  137. // catch (Exception ex)
  138. // {
  139. // current = DefaultEdgeDownloadFolder;
  140. // ErrorLogger.LogError("Utilities.GetEdgeDownloadFolder", ex.Message, ex.StackTrace);
  141. // }
  142. // return current;
  143. //}
  144. // DEPRECATED
  145. //internal static void SetEdgeDownloadFolder(string path)
  146. //{
  147. // Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge", "DownloadDirectory", path, RegistryValueKind.String);
  148. //}
  149. internal static void RunBatchFile(string batchFile)
  150. {
  151. try
  152. {
  153. using (Process p = new Process())
  154. {
  155. p.StartInfo.CreateNoWindow = true;
  156. p.StartInfo.FileName = batchFile;
  157. p.StartInfo.UseShellExecute = false;
  158. p.Start();
  159. p.WaitForExit();
  160. p.Close();
  161. }
  162. }
  163. catch (Exception ex)
  164. {
  165. ErrorLogger.LogError("Utilities.RunBatchFile", ex.Message, ex.StackTrace);
  166. }
  167. }
  168. internal static void ImportRegistryScript(string scriptFile)
  169. {
  170. string path = "\"" + scriptFile + "\"";
  171. Process p = new Process();
  172. try
  173. {
  174. p.StartInfo.FileName = "regedit.exe";
  175. p.StartInfo.UseShellExecute = false;
  176. p = Process.Start("regedit.exe", "/s " + path);
  177. p.WaitForExit();
  178. }
  179. catch (Exception ex)
  180. {
  181. p.Dispose();
  182. ErrorLogger.LogError("Utilities.ImportRegistryScript", ex.Message, ex.StackTrace);
  183. }
  184. finally
  185. {
  186. p.Dispose();
  187. }
  188. }
  189. internal static void Reboot()
  190. {
  191. Options.SaveSettings();
  192. Process.Start("shutdown /r /t 0");
  193. }
  194. internal static void DisableHibernation()
  195. {
  196. Utilities.RunCommand("powercfg -h off");
  197. Utilities.RunCommand("powercfg -h off");
  198. }
  199. internal static void EnableHibernation()
  200. {
  201. Utilities.RunCommand("powercfg -h on");
  202. Utilities.RunCommand("powercfg -h on");
  203. }
  204. internal static void ActivateMainForm()
  205. {
  206. Program._MainForm.Activate();
  207. }
  208. internal static bool ServiceExists(string serviceName)
  209. {
  210. return ServiceController.GetServices().Any(serviceController => serviceController.ServiceName.Equals(serviceName));
  211. }
  212. internal static void StopService(string serviceName)
  213. {
  214. if (ServiceExists(serviceName))
  215. {
  216. ServiceController sc = new ServiceController(serviceName);
  217. if (sc.CanStop)
  218. {
  219. sc.Stop();
  220. }
  221. }
  222. }
  223. internal static void StartService(string serviceName)
  224. {
  225. if (ServiceExists(serviceName))
  226. {
  227. ServiceController sc = new ServiceController(serviceName);
  228. try
  229. {
  230. sc.Start();
  231. }
  232. catch (Exception ex)
  233. {
  234. ErrorLogger.LogError("Utilities.StartService", ex.Message, ex.StackTrace);
  235. }
  236. }
  237. }
  238. internal static void EnableFirewall()
  239. {
  240. RunCommand("netsh advfirewall set currentprofile state on");
  241. }
  242. internal static void EnableCommandPrompt()
  243. {
  244. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Policies\\Microsoft\\Windows\\System"))
  245. {
  246. key.SetValue("DisableCMD", 0, RegistryValueKind.DWord);
  247. }
  248. }
  249. internal static void EnableControlPanel()
  250. {
  251. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  252. {
  253. key.SetValue("NoControlPanel", 0, RegistryValueKind.DWord);
  254. }
  255. }
  256. internal static void EnableFolderOptions()
  257. {
  258. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  259. {
  260. key.SetValue("NoFolderOptions", 0, RegistryValueKind.DWord);
  261. }
  262. }
  263. internal static void EnableRunDialog()
  264. {
  265. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  266. {
  267. key.SetValue("NoRun", 0, RegistryValueKind.DWord);
  268. }
  269. }
  270. internal static void EnableContextMenu()
  271. {
  272. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  273. {
  274. key.SetValue("NoViewContextMenu", 0, RegistryValueKind.DWord);
  275. }
  276. }
  277. internal static void EnableTaskManager()
  278. {
  279. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"))
  280. {
  281. key.SetValue("DisableTaskMgr", 0, RegistryValueKind.DWord);
  282. }
  283. }
  284. internal static void EnableRegistryEditor()
  285. {
  286. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"))
  287. {
  288. key.SetValue("DisableRegistryTools", 0, RegistryValueKind.DWord);
  289. }
  290. }
  291. internal static void RunCommand(string command)
  292. {
  293. using (Process p = new Process())
  294. {
  295. p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  296. p.StartInfo.FileName = "cmd.exe";
  297. p.StartInfo.Arguments = "/C " + command;
  298. p.StartInfo.CreateNoWindow = true;
  299. try
  300. {
  301. p.Start();
  302. p.WaitForExit();
  303. p.Close();
  304. }
  305. catch (Exception ex)
  306. {
  307. ErrorLogger.LogError("Utilities.RunCommand", ex.Message, ex.StackTrace);
  308. }
  309. }
  310. }
  311. internal static void FindFile(string fileName)
  312. {
  313. if (File.Exists(fileName)) Process.Start("explorer.exe", $"/select, \"{fileName}\"");
  314. }
  315. internal static void FindFolder(string folder)
  316. {
  317. if (Directory.Exists(folder)) RunCommand($"explorer.exe \"{folder}\"");
  318. }
  319. internal static string GetShortcutTargetFile(string shortcutFilename)
  320. {
  321. string pathOnly = Path.GetDirectoryName(shortcutFilename);
  322. string filenameOnly = Path.GetFileName(shortcutFilename);
  323. Shell32.Shell shell = new Shell32.Shell();
  324. Shell32.Folder folder = shell.NameSpace(pathOnly);
  325. Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
  326. if (folderItem != null)
  327. {
  328. Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
  329. return link.Path;
  330. }
  331. return string.Empty;
  332. }
  333. internal static void RestartExplorer()
  334. {
  335. const string explorer = "explorer.exe";
  336. string explorerPath = string.Format("{0}\\{1}", Environment.GetEnvironmentVariable("WINDIR"), explorer);
  337. foreach (Process process in Process.GetProcesses())
  338. {
  339. try
  340. {
  341. if (string.Compare(process.MainModule.FileName, explorerPath, StringComparison.OrdinalIgnoreCase) == 0)
  342. {
  343. process.Kill();
  344. }
  345. }
  346. catch (Exception ex)
  347. {
  348. ErrorLogger.LogError("Utilities.RestartExplorer", ex.Message, ex.StackTrace);
  349. }
  350. }
  351. Thread.Sleep(TimeSpan.FromSeconds(1));
  352. Process.Start(explorer);
  353. }
  354. internal static void FindKeyInRegistry(string key)
  355. {
  356. try
  357. {
  358. Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit", "LastKey", key);
  359. Process.Start("regedit");
  360. }
  361. catch (Exception ex)
  362. {
  363. ErrorLogger.LogError("Utilities.FindKeyInRegistry", ex.Message, ex.StackTrace);
  364. }
  365. }
  366. internal static void ResetConfiguration(bool withoutRestart = false)
  367. {
  368. try
  369. {
  370. Directory.Delete(Required.CoreFolder, true);
  371. }
  372. catch (Exception ex)
  373. {
  374. ErrorLogger.LogError("Utilities.ResetConfiguration", ex.Message, ex.StackTrace);
  375. }
  376. finally
  377. {
  378. if (!withoutRestart)
  379. {
  380. // BYPASS SINGLE-INSTANCE MECHANISM
  381. if (Program.MUTEX != null)
  382. {
  383. Program.MUTEX.ReleaseMutex();
  384. Program.MUTEX.Dispose();
  385. Program.MUTEX = null;
  386. }
  387. Application.Restart();
  388. }
  389. }
  390. }
  391. internal static Task RunAsync(this Process process)
  392. {
  393. var tcs = new TaskCompletionSource<object>();
  394. process.EnableRaisingEvents = true;
  395. process.Exited += (s, e) => tcs.TrySetResult(null);
  396. if (!process.Start()) tcs.SetException(new Exception("Failed to start process."));
  397. return tcs.Task;
  398. }
  399. internal static string SanitizeFileFolderName(string fileName)
  400. {
  401. char[] invalids = Path.GetInvalidFileNameChars();
  402. return string.Join("_", fileName.Split(invalids, StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');
  403. }
  404. // attempt to enable Local Group Policy Editor on Windows 10 Home editions
  405. internal static void EnableGPEDitor()
  406. {
  407. Utilities.RunBatchFile(Required.ScriptsFolder + "GPEditEnablerInHome.bat");
  408. }
  409. internal static void TryDeleteRegistryValue(bool localMachine, string path, string valueName)
  410. {
  411. try
  412. {
  413. if (localMachine) Registry.LocalMachine.OpenSubKey(path, true).DeleteValue(valueName, false);
  414. if (!localMachine) Registry.CurrentUser.OpenSubKey(path, true).DeleteValue(valueName, false);
  415. }
  416. catch { }
  417. }
  418. internal static void TryDeleteRegistryValueDefaultUsers(string path, string valueName)
  419. {
  420. try
  421. {
  422. Registry.Users.OpenSubKey(path, true).DeleteValue(valueName, false);
  423. }
  424. catch { }
  425. }
  426. internal static void DisableProtectedService(string serviceName)
  427. {
  428. using (TokenPrivilege.TakeOwnership)
  429. {
  430. using (RegistryKey allServicesKey = Registry.LocalMachine.OpenSubKeyWritable(@"SYSTEM\CurrentControlSet\Services"))
  431. {
  432. allServicesKey.GrantFullControlOnSubKey(serviceName);
  433. using (RegistryKey serviceKey = allServicesKey.OpenSubKeyWritable(serviceName))
  434. {
  435. if (serviceKey == null) return;
  436. foreach (string subkeyName in serviceKey.GetSubKeyNames())
  437. {
  438. serviceKey.TakeOwnershipOnSubKey(subkeyName);
  439. serviceKey.GrantFullControlOnSubKey(subkeyName);
  440. }
  441. serviceKey.SetValue("Start", "4", RegistryValueKind.DWord);
  442. }
  443. }
  444. }
  445. }
  446. internal static void RestoreWindowsPhotoViewer()
  447. {
  448. const string PHOTO_VIEWER_SHELL_COMMAND =
  449. @"%SystemRoot%\System32\rundll32.exe ""%ProgramFiles%\Windows Photo Viewer\PhotoViewer.dll"", ImageView_Fullscreen %1";
  450. const string PHOTO_VIEWER_CLSID = "{FFE2A43C-56B9-4bf5-9A79-CC6D4285608A}";
  451. Registry.SetValue(@"HKEY_CLASSES_ROOT\Applications\photoviewer.dll\shell\open", "MuiVerb", "@photoviewer.dll,-3043");
  452. Registry.SetValue(
  453. @"HKEY_CLASSES_ROOT\Applications\photoviewer.dll\shell\open\command", valueName: null,
  454. PHOTO_VIEWER_SHELL_COMMAND, RegistryValueKind.ExpandString
  455. );
  456. Registry.SetValue(@"HKEY_CLASSES_ROOT\Applications\photoviewer.dll\shell\open\DropTarget", "Clsid", PHOTO_VIEWER_CLSID);
  457. string[] imageTypes = { "Paint.Picture", "giffile", "jpegfile", "pngfile" };
  458. foreach (string type in imageTypes)
  459. {
  460. Registry.SetValue(
  461. $@"HKEY_CLASSES_ROOT\{type}\shell\open\command", valueName: null,
  462. PHOTO_VIEWER_SHELL_COMMAND, RegistryValueKind.ExpandString
  463. );
  464. Registry.SetValue($@"HKEY_CLASSES_ROOT\{type}\shell\open\DropTarget", "Clsid", PHOTO_VIEWER_CLSID);
  465. }
  466. }
  467. internal static void EnableProtectedService(string serviceName)
  468. {
  469. using (TokenPrivilege.TakeOwnership)
  470. {
  471. using (RegistryKey allServicesKey = Registry.LocalMachine.OpenSubKeyWritable(@"SYSTEM\CurrentControlSet\Services"))
  472. {
  473. allServicesKey.GrantFullControlOnSubKey(serviceName);
  474. using (RegistryKey serviceKey = allServicesKey.OpenSubKeyWritable(serviceName))
  475. {
  476. if (serviceKey == null) return;
  477. foreach (string subkeyName in serviceKey.GetSubKeyNames())
  478. {
  479. serviceKey.TakeOwnershipOnSubKey(subkeyName);
  480. serviceKey.GrantFullControlOnSubKey(subkeyName);
  481. }
  482. serviceKey.SetValue("Start", "2", RegistryValueKind.DWord);
  483. }
  484. }
  485. }
  486. }
  487. public static RegistryKey OpenSubKeyWritable(this RegistryKey registryKey, string subkeyName, RegistryRights? rights = null)
  488. {
  489. RegistryKey subKey = null;
  490. if (rights == null)
  491. subKey = registryKey.OpenSubKey(subkeyName, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl);
  492. else
  493. subKey = registryKey.OpenSubKey(subkeyName, RegistryKeyPermissionCheck.ReadWriteSubTree, rights.Value);
  494. if (subKey == null)
  495. {
  496. ErrorLogger.LogError("Utilities.OpenSubKeyWritable", $"Subkey {subkeyName} not found.", "-");
  497. }
  498. return subKey;
  499. }
  500. internal static SecurityIdentifier RetrieveCurrentUserIdentifier()
  501. => WindowsIdentity.GetCurrent().User ?? throw new Exception("Unable to retrieve current user SID.");
  502. internal static void GrantFullControlOnSubKey(this RegistryKey registryKey, string subkeyName)
  503. {
  504. using (RegistryKey subKey = registryKey.OpenSubKeyWritable(subkeyName,
  505. RegistryRights.TakeOwnership | RegistryRights.ChangePermissions
  506. ))
  507. {
  508. RegistrySecurity accessRules = subKey.GetAccessControl();
  509. SecurityIdentifier currentUser = RetrieveCurrentUserIdentifier();
  510. accessRules.SetOwner(currentUser);
  511. accessRules.ResetAccessRule(
  512. new RegistryAccessRule(
  513. currentUser,
  514. RegistryRights.FullControl,
  515. InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
  516. PropagationFlags.None,
  517. AccessControlType.Allow
  518. )
  519. );
  520. subKey.SetAccessControl(accessRules);
  521. }
  522. }
  523. internal static void TakeOwnershipOnSubKey(this RegistryKey registryKey, string subkeyName)
  524. {
  525. using (RegistryKey subKey = registryKey.OpenSubKeyWritable(subkeyName, RegistryRights.TakeOwnership))
  526. {
  527. RegistrySecurity accessRules = subKey.GetAccessControl();
  528. accessRules.SetOwner(RetrieveCurrentUserIdentifier());
  529. subKey.SetAccessControl(accessRules);
  530. }
  531. }
  532. internal static string GetNETFramework()
  533. {
  534. string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
  535. int netRelease;
  536. using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
  537. {
  538. if (ndpKey != null && ndpKey.GetValue("Release") != null)
  539. {
  540. netRelease = (int)ndpKey.GetValue("Release");
  541. }
  542. else
  543. {
  544. return "4.0";
  545. }
  546. }
  547. if (netRelease >= 528040)
  548. return "4.8";
  549. if (netRelease >= 461808)
  550. return "4.7.2";
  551. if (netRelease >= 461308)
  552. return "4.7.1";
  553. if (netRelease >= 460798)
  554. return "4.7";
  555. if (netRelease >= 394802)
  556. return "4.6.2";
  557. if (netRelease >= 394254)
  558. return "4.6.1";
  559. if (netRelease >= 393295)
  560. return "4.6";
  561. if (netRelease >= 379893)
  562. return "4.5.2";
  563. if (netRelease >= 378675)
  564. return "4.5.1";
  565. if (netRelease >= 378389)
  566. return "4.5";
  567. return "4.0";
  568. }
  569. internal static void SearchWith(string term, bool ddg)
  570. {
  571. try
  572. {
  573. if (ddg) Process.Start(string.Format("https://duckduckgo.com/?q={0}", term));
  574. if (!ddg) Process.Start(string.Format("https://www.google.com/search?q={0}", term));
  575. }
  576. catch { }
  577. }
  578. // [!!!]
  579. internal static void UnlockAllCores()
  580. {
  581. try
  582. {
  583. Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\PowerSettings\54533251-82be-4824-96c1-47b60b740d00\0cc5b647-c1df-4637-891a-dec35c318583", "ValueMax", 0, RegistryValueKind.DWord);
  584. Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\PowerSettings\54533251-82be-4824-96c1-47b60b740d00\0cc5b647-c1df-4637-891a-dec35c318583", "ValueMin", 0, RegistryValueKind.DWord);
  585. }
  586. catch { }
  587. }
  588. internal static void DisableHPET()
  589. {
  590. Utilities.RunCommand("bcdedit /deletevalue useplatformclock");
  591. Thread.Sleep(500);
  592. Utilities.RunCommand("bcdedit /set disabledynamictick yes");
  593. }
  594. internal static void EnableHPET()
  595. {
  596. Utilities.RunCommand("bcdedit /set useplatformclock true");
  597. Thread.Sleep(500);
  598. Utilities.RunCommand("bcdedit /set disabledynamictick no");
  599. }
  600. // [!!!]
  601. //internal static void ChangeNumberOfSvcHostByRAM(string ram)
  602. //{
  603. // try
  604. // {
  605. // float kbs = float.Parse(ram);
  606. // Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control", "SvcHostSplitThresholdInKB", kbs * 1024 * 1024, RegistryValueKind.DWord);
  607. // }
  608. // catch { }
  609. //}
  610. internal static void AddToStartup()
  611. {
  612. try
  613. {
  614. using (RegistryKey k = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
  615. {
  616. k.SetValue("Optimizer", Assembly.GetEntryAssembly().Location);
  617. }
  618. }
  619. catch (Exception ex)
  620. {
  621. ErrorLogger.LogError("Utilities.AddToStartup", ex.Message, ex.StackTrace);
  622. }
  623. }
  624. internal static void DeleteFromStartup()
  625. {
  626. try
  627. {
  628. using (RegistryKey k = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
  629. {
  630. k.DeleteValue("Optimizer", false);
  631. }
  632. }
  633. catch (Exception ex)
  634. {
  635. ErrorLogger.LogError("Utilities.DeleteFromStartup", ex.Message, ex.StackTrace);
  636. }
  637. }
  638. internal static void PreventProcessFromRunning(string pName)
  639. {
  640. try
  641. {
  642. using (RegistryKey ifeo = Registry.LocalMachine.OpenSubKeyWritable(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", RegistryRights.FullControl))
  643. {
  644. if (ifeo == null) return;
  645. ifeo.GrantFullControlOnSubKey("Image File Execution Options");
  646. using (RegistryKey k = ifeo.OpenSubKeyWritable("Image File Execution Options", RegistryRights.FullControl))
  647. {
  648. if (k == null) return;
  649. k.CreateSubKey(pName);
  650. k.GrantFullControlOnSubKey(pName);
  651. using (RegistryKey f = k.OpenSubKeyWritable(pName, RegistryRights.FullControl))
  652. {
  653. if (f == null) return;
  654. f.SetValue("Debugger", @"%windir%\System32\taskkill.exe");
  655. }
  656. }
  657. }
  658. }
  659. catch (Exception ex)
  660. {
  661. ErrorLogger.LogError("Utilities.PreventProcessFromRunning", ex.Message, ex.StackTrace);
  662. }
  663. }
  664. }
  665. }