Utilities.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. Utilities.RunCommand("shutdown /r /t 0");
  192. }
  193. internal static void DisableHibernation()
  194. {
  195. Utilities.RunCommand("powercfg -h off");
  196. Utilities.RunCommand("powercfg -h off");
  197. }
  198. internal static void EnableHibernation()
  199. {
  200. Utilities.RunCommand("powercfg -h on");
  201. Utilities.RunCommand("powercfg -h on");
  202. }
  203. internal static void ActivateMainForm()
  204. {
  205. Program._MainForm.Activate();
  206. }
  207. internal static bool ServiceExists(string serviceName)
  208. {
  209. return ServiceController.GetServices().Any(serviceController => serviceController.ServiceName.Equals(serviceName));
  210. }
  211. internal static void StopService(string serviceName)
  212. {
  213. if (ServiceExists(serviceName))
  214. {
  215. ServiceController sc = new ServiceController(serviceName);
  216. if (sc.CanStop)
  217. {
  218. sc.Stop();
  219. }
  220. }
  221. }
  222. internal static void StartService(string serviceName)
  223. {
  224. if (ServiceExists(serviceName))
  225. {
  226. ServiceController sc = new ServiceController(serviceName);
  227. try
  228. {
  229. sc.Start();
  230. }
  231. catch (Exception ex)
  232. {
  233. ErrorLogger.LogError("Utilities.StartService", ex.Message, ex.StackTrace);
  234. }
  235. }
  236. }
  237. internal static void EnableFirewall()
  238. {
  239. RunCommand("netsh advfirewall set currentprofile state on");
  240. }
  241. internal static void EnableCommandPrompt()
  242. {
  243. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Policies\\Microsoft\\Windows\\System"))
  244. {
  245. key.SetValue("DisableCMD", 0, RegistryValueKind.DWord);
  246. }
  247. }
  248. internal static void EnableControlPanel()
  249. {
  250. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  251. {
  252. key.SetValue("NoControlPanel", 0, RegistryValueKind.DWord);
  253. }
  254. }
  255. internal static void EnableFolderOptions()
  256. {
  257. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  258. {
  259. key.SetValue("NoFolderOptions", 0, RegistryValueKind.DWord);
  260. }
  261. }
  262. internal static void EnableRunDialog()
  263. {
  264. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  265. {
  266. key.SetValue("NoRun", 0, RegistryValueKind.DWord);
  267. }
  268. }
  269. internal static void EnableContextMenu()
  270. {
  271. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  272. {
  273. key.SetValue("NoViewContextMenu", 0, RegistryValueKind.DWord);
  274. }
  275. }
  276. internal static void EnableTaskManager()
  277. {
  278. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"))
  279. {
  280. key.SetValue("DisableTaskMgr", 0, RegistryValueKind.DWord);
  281. }
  282. }
  283. internal static void EnableRegistryEditor()
  284. {
  285. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"))
  286. {
  287. key.SetValue("DisableRegistryTools", 0, RegistryValueKind.DWord);
  288. }
  289. }
  290. internal static void RunCommand(string command)
  291. {
  292. using (Process p = new Process())
  293. {
  294. p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  295. p.StartInfo.FileName = "cmd.exe";
  296. p.StartInfo.Arguments = "/C " + command;
  297. p.StartInfo.CreateNoWindow = true;
  298. try
  299. {
  300. p.Start();
  301. p.WaitForExit();
  302. p.Close();
  303. }
  304. catch (Exception ex)
  305. {
  306. ErrorLogger.LogError("Utilities.RunCommand", ex.Message, ex.StackTrace);
  307. }
  308. }
  309. }
  310. internal static void FindFile(string fileName)
  311. {
  312. if (File.Exists(fileName)) Process.Start("explorer.exe", $"/select, \"{fileName}\"");
  313. }
  314. internal static void FindFolder(string folder)
  315. {
  316. if (Directory.Exists(folder)) RunCommand($"explorer.exe \"{folder}\"");
  317. }
  318. internal static string GetShortcutTargetFile(string shortcutFilename)
  319. {
  320. string pathOnly = Path.GetDirectoryName(shortcutFilename);
  321. string filenameOnly = Path.GetFileName(shortcutFilename);
  322. Shell32.Shell shell = new Shell32.Shell();
  323. Shell32.Folder folder = shell.NameSpace(pathOnly);
  324. Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
  325. if (folderItem != null)
  326. {
  327. Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
  328. return link.Path;
  329. }
  330. return string.Empty;
  331. }
  332. internal static void RestartExplorer()
  333. {
  334. const string explorer = "explorer.exe";
  335. string explorerPath = string.Format("{0}\\{1}", Environment.GetEnvironmentVariable("WINDIR"), explorer);
  336. foreach (Process process in Process.GetProcesses())
  337. {
  338. try
  339. {
  340. if (string.Compare(process.MainModule.FileName, explorerPath, StringComparison.OrdinalIgnoreCase) == 0)
  341. {
  342. process.Kill();
  343. }
  344. }
  345. catch (Exception ex)
  346. {
  347. ErrorLogger.LogError("Utilities.RestartExplorer", ex.Message, ex.StackTrace);
  348. }
  349. }
  350. Thread.Sleep(TimeSpan.FromSeconds(1));
  351. Process.Start(explorer);
  352. }
  353. internal static void FindKeyInRegistry(string key)
  354. {
  355. try
  356. {
  357. Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit", "LastKey", key);
  358. Process.Start("regedit");
  359. }
  360. catch (Exception ex)
  361. {
  362. ErrorLogger.LogError("Utilities.FindKeyInRegistry", ex.Message, ex.StackTrace);
  363. }
  364. }
  365. internal static void ResetConfiguration(bool withoutRestart = false)
  366. {
  367. try
  368. {
  369. Directory.Delete(Required.CoreFolder, true);
  370. }
  371. catch (Exception ex)
  372. {
  373. ErrorLogger.LogError("Utilities.ResetConfiguration", ex.Message, ex.StackTrace);
  374. }
  375. finally
  376. {
  377. if (!withoutRestart)
  378. {
  379. // BYPASS SINGLE-INSTANCE MECHANISM
  380. if (Program.MUTEX != null)
  381. {
  382. Program.MUTEX.ReleaseMutex();
  383. Program.MUTEX.Dispose();
  384. Program.MUTEX = null;
  385. }
  386. Application.Restart();
  387. }
  388. }
  389. }
  390. internal static Task RunAsync(this Process process)
  391. {
  392. var tcs = new TaskCompletionSource<object>();
  393. process.EnableRaisingEvents = true;
  394. process.Exited += (s, e) => tcs.TrySetResult(null);
  395. if (!process.Start()) tcs.SetException(new Exception("Failed to start process."));
  396. return tcs.Task;
  397. }
  398. internal static string SanitizeFileFolderName(string fileName)
  399. {
  400. char[] invalids = Path.GetInvalidFileNameChars();
  401. return string.Join("_", fileName.Split(invalids, StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');
  402. }
  403. // attempt to enable Local Group Policy Editor on Windows 10 Home editions
  404. internal static void EnableGPEDitor()
  405. {
  406. Utilities.RunBatchFile(Required.ScriptsFolder + "GPEditEnablerInHome.bat");
  407. }
  408. internal static void TryDeleteRegistryValue(bool localMachine, string path, string valueName)
  409. {
  410. try
  411. {
  412. if (localMachine) Registry.LocalMachine.OpenSubKey(path, true).DeleteValue(valueName, false);
  413. if (!localMachine) Registry.CurrentUser.OpenSubKey(path, true).DeleteValue(valueName, false);
  414. }
  415. catch { }
  416. }
  417. internal static void TryDeleteRegistryValueDefaultUsers(string path, string valueName)
  418. {
  419. try
  420. {
  421. Registry.Users.OpenSubKey(path, true).DeleteValue(valueName, false);
  422. }
  423. catch { }
  424. }
  425. internal static void DisableProtectedService(string serviceName)
  426. {
  427. using (TokenPrivilege.TakeOwnership)
  428. {
  429. using (RegistryKey allServicesKey = Registry.LocalMachine.OpenSubKeyWritable(@"SYSTEM\CurrentControlSet\Services"))
  430. {
  431. allServicesKey.GrantFullControlOnSubKey(serviceName);
  432. using (RegistryKey serviceKey = allServicesKey.OpenSubKeyWritable(serviceName))
  433. {
  434. if (serviceKey == null) return;
  435. foreach (string subkeyName in serviceKey.GetSubKeyNames())
  436. {
  437. serviceKey.TakeOwnershipOnSubKey(subkeyName);
  438. serviceKey.GrantFullControlOnSubKey(subkeyName);
  439. }
  440. serviceKey.SetValue("Start", "4", RegistryValueKind.DWord);
  441. }
  442. }
  443. }
  444. }
  445. internal static void RestoreWindowsPhotoViewer()
  446. {
  447. const string PHOTO_VIEWER_SHELL_COMMAND =
  448. @"%SystemRoot%\System32\rundll32.exe ""%ProgramFiles%\Windows Photo Viewer\PhotoViewer.dll"", ImageView_Fullscreen %1";
  449. const string PHOTO_VIEWER_CLSID = "{FFE2A43C-56B9-4bf5-9A79-CC6D4285608A}";
  450. Registry.SetValue(@"HKEY_CLASSES_ROOT\Applications\photoviewer.dll\shell\open", "MuiVerb", "@photoviewer.dll,-3043");
  451. Registry.SetValue(
  452. @"HKEY_CLASSES_ROOT\Applications\photoviewer.dll\shell\open\command", valueName: null,
  453. PHOTO_VIEWER_SHELL_COMMAND, RegistryValueKind.ExpandString
  454. );
  455. Registry.SetValue(@"HKEY_CLASSES_ROOT\Applications\photoviewer.dll\shell\open\DropTarget", "Clsid", PHOTO_VIEWER_CLSID);
  456. string[] imageTypes = { "Paint.Picture", "giffile", "jpegfile", "pngfile" };
  457. foreach (string type in imageTypes)
  458. {
  459. Registry.SetValue(
  460. $@"HKEY_CLASSES_ROOT\{type}\shell\open\command", valueName: null,
  461. PHOTO_VIEWER_SHELL_COMMAND, RegistryValueKind.ExpandString
  462. );
  463. Registry.SetValue($@"HKEY_CLASSES_ROOT\{type}\shell\open\DropTarget", "Clsid", PHOTO_VIEWER_CLSID);
  464. }
  465. }
  466. internal static void EnableProtectedService(string serviceName)
  467. {
  468. using (TokenPrivilege.TakeOwnership)
  469. {
  470. using (RegistryKey allServicesKey = Registry.LocalMachine.OpenSubKeyWritable(@"SYSTEM\CurrentControlSet\Services"))
  471. {
  472. allServicesKey.GrantFullControlOnSubKey(serviceName);
  473. using (RegistryKey serviceKey = allServicesKey.OpenSubKeyWritable(serviceName))
  474. {
  475. if (serviceKey == null) return;
  476. foreach (string subkeyName in serviceKey.GetSubKeyNames())
  477. {
  478. serviceKey.TakeOwnershipOnSubKey(subkeyName);
  479. serviceKey.GrantFullControlOnSubKey(subkeyName);
  480. }
  481. serviceKey.SetValue("Start", "2", RegistryValueKind.DWord);
  482. }
  483. }
  484. }
  485. }
  486. public static RegistryKey OpenSubKeyWritable(this RegistryKey registryKey, string subkeyName, RegistryRights? rights = null)
  487. {
  488. RegistryKey subKey = null;
  489. if (rights == null)
  490. subKey = registryKey.OpenSubKey(subkeyName, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl);
  491. else
  492. subKey = registryKey.OpenSubKey(subkeyName, RegistryKeyPermissionCheck.ReadWriteSubTree, rights.Value);
  493. if (subKey == null)
  494. {
  495. ErrorLogger.LogError("Utilities.OpenSubKeyWritable", $"Subkey {subkeyName} not found.", "-");
  496. }
  497. return subKey;
  498. }
  499. internal static SecurityIdentifier RetrieveCurrentUserIdentifier()
  500. => WindowsIdentity.GetCurrent().User ?? throw new Exception("Unable to retrieve current user SID.");
  501. internal static void GrantFullControlOnSubKey(this RegistryKey registryKey, string subkeyName)
  502. {
  503. using (RegistryKey subKey = registryKey.OpenSubKeyWritable(subkeyName,
  504. RegistryRights.TakeOwnership | RegistryRights.ChangePermissions
  505. ))
  506. {
  507. RegistrySecurity accessRules = subKey.GetAccessControl();
  508. SecurityIdentifier currentUser = RetrieveCurrentUserIdentifier();
  509. accessRules.SetOwner(currentUser);
  510. accessRules.ResetAccessRule(
  511. new RegistryAccessRule(
  512. currentUser,
  513. RegistryRights.FullControl,
  514. InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
  515. PropagationFlags.None,
  516. AccessControlType.Allow
  517. )
  518. );
  519. subKey.SetAccessControl(accessRules);
  520. }
  521. }
  522. internal static void TakeOwnershipOnSubKey(this RegistryKey registryKey, string subkeyName)
  523. {
  524. using (RegistryKey subKey = registryKey.OpenSubKeyWritable(subkeyName, RegistryRights.TakeOwnership))
  525. {
  526. RegistrySecurity accessRules = subKey.GetAccessControl();
  527. accessRules.SetOwner(RetrieveCurrentUserIdentifier());
  528. subKey.SetAccessControl(accessRules);
  529. }
  530. }
  531. internal static string GetNETFramework()
  532. {
  533. string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
  534. int netRelease;
  535. using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
  536. {
  537. if (ndpKey != null && ndpKey.GetValue("Release") != null)
  538. {
  539. netRelease = (int)ndpKey.GetValue("Release");
  540. }
  541. else
  542. {
  543. return "4.0";
  544. }
  545. }
  546. if (netRelease >= 528040)
  547. return "4.8";
  548. if (netRelease >= 461808)
  549. return "4.7.2";
  550. if (netRelease >= 461308)
  551. return "4.7.1";
  552. if (netRelease >= 460798)
  553. return "4.7";
  554. if (netRelease >= 394802)
  555. return "4.6.2";
  556. if (netRelease >= 394254)
  557. return "4.6.1";
  558. if (netRelease >= 393295)
  559. return "4.6";
  560. if (netRelease >= 379893)
  561. return "4.5.2";
  562. if (netRelease >= 378675)
  563. return "4.5.1";
  564. if (netRelease >= 378389)
  565. return "4.5";
  566. return "4.0";
  567. }
  568. internal static void SearchWith(string term, bool ddg)
  569. {
  570. try
  571. {
  572. if (ddg) Process.Start(string.Format("https://duckduckgo.com/?q={0}", term));
  573. if (!ddg) Process.Start(string.Format("https://www.google.com/search?q={0}", term));
  574. }
  575. catch { }
  576. }
  577. // [!!!]
  578. internal static void UnlockAllCores()
  579. {
  580. try
  581. {
  582. Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\PowerSettings\54533251-82be-4824-96c1-47b60b740d00\0cc5b647-c1df-4637-891a-dec35c318583", "ValueMax", 0, RegistryValueKind.DWord);
  583. Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\PowerSettings\54533251-82be-4824-96c1-47b60b740d00\0cc5b647-c1df-4637-891a-dec35c318583", "ValueMin", 0, RegistryValueKind.DWord);
  584. }
  585. catch { }
  586. }
  587. internal static void DisableHPET()
  588. {
  589. Utilities.RunCommand("bcdedit /deletevalue useplatformclock");
  590. Thread.Sleep(500);
  591. Utilities.RunCommand("bcdedit /set disabledynamictick yes");
  592. }
  593. internal static void EnableHPET()
  594. {
  595. Utilities.RunCommand("bcdedit /set useplatformclock true");
  596. Thread.Sleep(500);
  597. Utilities.RunCommand("bcdedit /set disabledynamictick no");
  598. }
  599. // [!!!]
  600. //internal static void ChangeNumberOfSvcHostByRAM(string ram)
  601. //{
  602. // try
  603. // {
  604. // float kbs = float.Parse(ram);
  605. // Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control", "SvcHostSplitThresholdInKB", kbs * 1024 * 1024, RegistryValueKind.DWord);
  606. // }
  607. // catch { }
  608. //}
  609. internal static void AddToStartup()
  610. {
  611. try
  612. {
  613. using (RegistryKey k = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
  614. {
  615. k.SetValue("Optimizer", Assembly.GetEntryAssembly().Location);
  616. }
  617. }
  618. catch (Exception ex)
  619. {
  620. ErrorLogger.LogError("Utilities.AddToStartup", ex.Message, ex.StackTrace);
  621. }
  622. }
  623. internal static void DeleteFromStartup()
  624. {
  625. try
  626. {
  627. using (RegistryKey k = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
  628. {
  629. k.DeleteValue("Optimizer", false);
  630. }
  631. }
  632. catch (Exception ex)
  633. {
  634. ErrorLogger.LogError("Utilities.DeleteFromStartup", ex.Message, ex.StackTrace);
  635. }
  636. }
  637. internal static void PreventProcessFromRunning(string pName)
  638. {
  639. try
  640. {
  641. using (RegistryKey ifeo = Registry.LocalMachine.OpenSubKeyWritable(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", RegistryRights.FullControl))
  642. {
  643. if (ifeo == null) return;
  644. ifeo.GrantFullControlOnSubKey("Image File Execution Options");
  645. using (RegistryKey k = ifeo.OpenSubKeyWritable("Image File Execution Options", RegistryRights.FullControl))
  646. {
  647. if (k == null) return;
  648. k.CreateSubKey(pName);
  649. k.GrantFullControlOnSubKey(pName);
  650. using (RegistryKey f = k.OpenSubKeyWritable(pName, RegistryRights.FullControl))
  651. {
  652. if (f == null) return;
  653. f.SetValue("Debugger", @"%windir%\System32\taskkill.exe");
  654. }
  655. }
  656. }
  657. }
  658. catch (Exception ex)
  659. {
  660. ErrorLogger.LogError("Utilities.PreventProcessFromRunning", ex.Message, ex.StackTrace);
  661. }
  662. }
  663. }
  664. }