Utilities.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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 string GetShortcutTargetFile(string shortcutFilename)
  315. {
  316. string pathOnly = Path.GetDirectoryName(shortcutFilename);
  317. string filenameOnly = Path.GetFileName(shortcutFilename);
  318. Shell32.Shell shell = new Shell32.Shell();
  319. Shell32.Folder folder = shell.NameSpace(pathOnly);
  320. Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
  321. if (folderItem != null)
  322. {
  323. Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
  324. return link.Path;
  325. }
  326. return string.Empty;
  327. }
  328. internal static void RestartExplorer()
  329. {
  330. const string explorer = "explorer.exe";
  331. string explorerPath = string.Format("{0}\\{1}", Environment.GetEnvironmentVariable("WINDIR"), explorer);
  332. foreach (Process process in Process.GetProcesses())
  333. {
  334. try
  335. {
  336. if (string.Compare(process.MainModule.FileName, explorerPath, StringComparison.OrdinalIgnoreCase) == 0)
  337. {
  338. process.Kill();
  339. }
  340. }
  341. catch (Exception ex)
  342. {
  343. ErrorLogger.LogError("Utilities.RestartExplorer", ex.Message, ex.StackTrace);
  344. }
  345. }
  346. Thread.Sleep(TimeSpan.FromSeconds(1));
  347. Process.Start(explorer);
  348. }
  349. internal static void FindKeyInRegistry(string key)
  350. {
  351. try
  352. {
  353. Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit", "LastKey", key);
  354. Process.Start("regedit");
  355. }
  356. catch (Exception ex)
  357. {
  358. ErrorLogger.LogError("Utilities.FindKeyInRegistry", ex.Message, ex.StackTrace);
  359. }
  360. }
  361. internal static void ResetConfiguration(bool withoutRestart = false)
  362. {
  363. try
  364. {
  365. Directory.Delete(Required.CoreFolder, true);
  366. }
  367. catch (Exception ex)
  368. {
  369. ErrorLogger.LogError("Utilities.ResetConfiguration", ex.Message, ex.StackTrace);
  370. }
  371. finally
  372. {
  373. if (!withoutRestart)
  374. {
  375. // BYPASS SINGLE-INSTANCE MECHANISM
  376. if (Program.MUTEX != null)
  377. {
  378. Program.MUTEX.ReleaseMutex();
  379. Program.MUTEX.Dispose();
  380. Program.MUTEX = null;
  381. }
  382. Application.Restart();
  383. }
  384. }
  385. }
  386. internal static Task RunAsync(this Process process)
  387. {
  388. var tcs = new TaskCompletionSource<object>();
  389. process.EnableRaisingEvents = true;
  390. process.Exited += (s, e) => tcs.TrySetResult(null);
  391. if (!process.Start()) tcs.SetException(new Exception("Failed to start process."));
  392. return tcs.Task;
  393. }
  394. internal static string SanitizeFileFolderName(string fileName)
  395. {
  396. char[] invalids = Path.GetInvalidFileNameChars();
  397. return string.Join("_", fileName.Split(invalids, StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');
  398. }
  399. // attempt to enable Local Group Policy Editor on Windows 10 Home editions
  400. internal static void EnableGPEDitor()
  401. {
  402. Utilities.RunBatchFile(Required.ScriptsFolder + "GPEditEnablerInHome.bat");
  403. }
  404. internal static void TryDeleteRegistryValue(bool localMachine, string path, string valueName)
  405. {
  406. try
  407. {
  408. if (localMachine) Registry.LocalMachine.OpenSubKey(path, true).DeleteValue(valueName, false);
  409. if (!localMachine) Registry.CurrentUser.OpenSubKey(path, true).DeleteValue(valueName, false);
  410. }
  411. catch { }
  412. }
  413. internal static void DisableProtectedService(string serviceName)
  414. {
  415. using (TokenPrivilege.TakeOwnership)
  416. {
  417. using (RegistryKey allServicesKey = Registry.LocalMachine.OpenSubKeyWritable(@"SYSTEM\CurrentControlSet\Services"))
  418. {
  419. allServicesKey.GrantFullControlOnSubKey(serviceName);
  420. using (RegistryKey serviceKey = allServicesKey.OpenSubKeyWritable(serviceName))
  421. {
  422. if (serviceKey == null) return;
  423. foreach (string subkeyName in serviceKey.GetSubKeyNames())
  424. {
  425. serviceKey.TakeOwnershipOnSubKey(subkeyName);
  426. serviceKey.GrantFullControlOnSubKey(subkeyName);
  427. }
  428. serviceKey.SetValue("Start", "4", RegistryValueKind.DWord);
  429. }
  430. }
  431. }
  432. }
  433. internal static void RestoreWindowsPhotoViewer()
  434. {
  435. const string PHOTO_VIEWER_SHELL_COMMAND =
  436. @"%SystemRoot%\System32\rundll32.exe ""%ProgramFiles%\Windows Photo Viewer\PhotoViewer.dll"", ImageView_Fullscreen %1";
  437. const string PHOTO_VIEWER_CLSID = "{FFE2A43C-56B9-4bf5-9A79-CC6D4285608A}";
  438. Registry.SetValue(@"HKEY_CLASSES_ROOT\Applications\photoviewer.dll\shell\open", "MuiVerb", "@photoviewer.dll,-3043");
  439. Registry.SetValue(
  440. @"HKEY_CLASSES_ROOT\Applications\photoviewer.dll\shell\open\command", valueName: null,
  441. PHOTO_VIEWER_SHELL_COMMAND, RegistryValueKind.ExpandString
  442. );
  443. Registry.SetValue(@"HKEY_CLASSES_ROOT\Applications\photoviewer.dll\shell\open\DropTarget", "Clsid", PHOTO_VIEWER_CLSID);
  444. string[] imageTypes = { "Paint.Picture", "giffile", "jpegfile", "pngfile" };
  445. foreach (string type in imageTypes)
  446. {
  447. Registry.SetValue(
  448. $@"HKEY_CLASSES_ROOT\{type}\shell\open\command", valueName: null,
  449. PHOTO_VIEWER_SHELL_COMMAND, RegistryValueKind.ExpandString
  450. );
  451. Registry.SetValue($@"HKEY_CLASSES_ROOT\{type}\shell\open\DropTarget", "Clsid", PHOTO_VIEWER_CLSID);
  452. }
  453. }
  454. internal static void EnableProtectedService(string serviceName)
  455. {
  456. using (TokenPrivilege.TakeOwnership)
  457. {
  458. using (RegistryKey allServicesKey = Registry.LocalMachine.OpenSubKeyWritable(@"SYSTEM\CurrentControlSet\Services"))
  459. {
  460. allServicesKey.GrantFullControlOnSubKey(serviceName);
  461. using (RegistryKey serviceKey = allServicesKey.OpenSubKeyWritable(serviceName))
  462. {
  463. if (serviceKey == null) return;
  464. foreach (string subkeyName in serviceKey.GetSubKeyNames())
  465. {
  466. serviceKey.TakeOwnershipOnSubKey(subkeyName);
  467. serviceKey.GrantFullControlOnSubKey(subkeyName);
  468. }
  469. serviceKey.SetValue("Start", "2", RegistryValueKind.DWord);
  470. }
  471. }
  472. }
  473. }
  474. public static RegistryKey OpenSubKeyWritable(this RegistryKey registryKey, string subkeyName, RegistryRights? rights = null)
  475. {
  476. RegistryKey subKey = null;
  477. if (rights == null)
  478. subKey = registryKey.OpenSubKey(subkeyName, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl);
  479. else
  480. subKey = registryKey.OpenSubKey(subkeyName, RegistryKeyPermissionCheck.ReadWriteSubTree, rights.Value);
  481. if (subKey == null)
  482. {
  483. ErrorLogger.LogError("Utilities.OpenSubKeyWritable", $"Subkey {subkeyName} not found.", "-");
  484. }
  485. return subKey;
  486. }
  487. internal static SecurityIdentifier RetrieveCurrentUserIdentifier()
  488. => WindowsIdentity.GetCurrent().User ?? throw new Exception("Unable to retrieve current user SID.");
  489. internal static void GrantFullControlOnSubKey(this RegistryKey registryKey, string subkeyName)
  490. {
  491. using (RegistryKey subKey = registryKey.OpenSubKeyWritable(subkeyName,
  492. RegistryRights.TakeOwnership | RegistryRights.ChangePermissions
  493. ))
  494. {
  495. RegistrySecurity accessRules = subKey.GetAccessControl();
  496. SecurityIdentifier currentUser = RetrieveCurrentUserIdentifier();
  497. accessRules.SetOwner(currentUser);
  498. accessRules.ResetAccessRule(
  499. new RegistryAccessRule(
  500. currentUser,
  501. RegistryRights.FullControl,
  502. InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
  503. PropagationFlags.None,
  504. AccessControlType.Allow
  505. )
  506. );
  507. subKey.SetAccessControl(accessRules);
  508. }
  509. }
  510. internal static void TakeOwnershipOnSubKey(this RegistryKey registryKey, string subkeyName)
  511. {
  512. using (RegistryKey subKey = registryKey.OpenSubKeyWritable(subkeyName, RegistryRights.TakeOwnership))
  513. {
  514. RegistrySecurity accessRules = subKey.GetAccessControl();
  515. accessRules.SetOwner(RetrieveCurrentUserIdentifier());
  516. subKey.SetAccessControl(accessRules);
  517. }
  518. }
  519. internal static string GetNETFramework()
  520. {
  521. string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
  522. int netRelease;
  523. using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
  524. {
  525. if (ndpKey != null && ndpKey.GetValue("Release") != null)
  526. {
  527. netRelease = (int)ndpKey.GetValue("Release");
  528. }
  529. else
  530. {
  531. return "4.0";
  532. }
  533. }
  534. if (netRelease >= 528040)
  535. return "4.8";
  536. if (netRelease >= 461808)
  537. return "4.7.2";
  538. if (netRelease >= 461308)
  539. return "4.7.1";
  540. if (netRelease >= 460798)
  541. return "4.7";
  542. if (netRelease >= 394802)
  543. return "4.6.2";
  544. if (netRelease >= 394254)
  545. return "4.6.1";
  546. if (netRelease >= 393295)
  547. return "4.6";
  548. if (netRelease >= 379893)
  549. return "4.5.2";
  550. if (netRelease >= 378675)
  551. return "4.5.1";
  552. if (netRelease >= 378389)
  553. return "4.5";
  554. return "4.0";
  555. }
  556. internal static void SearchWith(string term, bool ddg)
  557. {
  558. try
  559. {
  560. if (ddg) Process.Start(string.Format("https://duckduckgo.com/?q={0}", term));
  561. if (!ddg) Process.Start(string.Format("https://www.google.com/search?q={0}", term));
  562. }
  563. catch { }
  564. }
  565. // [!!!]
  566. internal static void UnlockAllCores()
  567. {
  568. try
  569. {
  570. Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\PowerSettings\54533251-82be-4824-96c1-47b60b740d00\0cc5b647-c1df-4637-891a-dec35c318583", "ValueMax", 0, RegistryValueKind.DWord);
  571. Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\PowerSettings\54533251-82be-4824-96c1-47b60b740d00\0cc5b647-c1df-4637-891a-dec35c318583", "ValueMin", 0, RegistryValueKind.DWord);
  572. }
  573. catch { }
  574. }
  575. // [!!!]
  576. //internal static void ChangeNumberOfSvcHostByRAM(string ram)
  577. //{
  578. // try
  579. // {
  580. // float kbs = float.Parse(ram);
  581. // Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control", "SvcHostSplitThresholdInKB", kbs * 1024 * 1024, RegistryValueKind.DWord);
  582. // }
  583. // catch { }
  584. //}
  585. internal static void PreventProcessFromRunning(string pName)
  586. {
  587. try
  588. {
  589. using (RegistryKey ifeo = Registry.LocalMachine.OpenSubKeyWritable(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", RegistryRights.FullControl))
  590. {
  591. if (ifeo == null) return;
  592. ifeo.GrantFullControlOnSubKey("Image File Execution Options");
  593. using (RegistryKey k = ifeo.OpenSubKeyWritable("Image File Execution Options", RegistryRights.FullControl))
  594. {
  595. if (k == null) return;
  596. k.CreateSubKey(pName);
  597. k.GrantFullControlOnSubKey(pName);
  598. using (RegistryKey f = k.OpenSubKeyWritable(pName, RegistryRights.FullControl))
  599. {
  600. if (f == null) return;
  601. f.SetValue("Debugger", @"%windir%\System32\taskkill.exe");
  602. }
  603. }
  604. }
  605. }
  606. catch (Exception ex)
  607. {
  608. ErrorLogger.LogError("Utilities.PreventProcessFromRunning", ex.Message, ex.StackTrace);
  609. }
  610. }
  611. }
  612. }