Utilities.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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.Management.Automation;
  9. using System.Net;
  10. using System.Net.NetworkInformation;
  11. using System.Net.Sockets;
  12. using System.Reflection;
  13. using System.Security.Principal;
  14. using System.ServiceProcess;
  15. using System.Threading.Tasks;
  16. using System.Windows.Forms;
  17. namespace Optimizer
  18. {
  19. internal static class Utilities
  20. {
  21. internal static readonly string LocalMachineRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
  22. internal static readonly string LocalMachineRunOnce = "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
  23. internal static readonly string LocalMachineRunWoW = "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run";
  24. internal static readonly string LocalMachineRunOnceWow = "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
  25. internal static readonly string CurrentUserRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
  26. internal static readonly string CurrentUserRunOnce = "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
  27. internal static readonly string LocalMachineStartupFolder = CleanHelper.ProgramData + "\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
  28. internal static readonly string CurrentUserStartupFolder = CleanHelper.ProfileAppDataRoaming + "\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
  29. // DEPRECATED
  30. //internal readonly static string DefaultEdgeDownloadFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads");
  31. internal static WindowsVersion CurrentWindowsVersion = WindowsVersion.Unsupported;
  32. internal static Ping pinger = new Ping();
  33. static IPAddress addressToPing;
  34. internal delegate void SetControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue);
  35. internal static void SetControlPropertyThreadSafe(Control control, string propertyName, object propertyValue)
  36. {
  37. if (control.InvokeRequired)
  38. {
  39. control.Invoke(new SetControlPropertyThreadSafeDelegate(SetControlPropertyThreadSafe), new object[] { control, propertyName, propertyValue });
  40. }
  41. else
  42. {
  43. control.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, control, new object[] { propertyValue });
  44. }
  45. }
  46. internal static IEnumerable<Control> GetSelfAndChildrenRecursive(Control parent)
  47. {
  48. List<Control> controls = new List<Control>();
  49. foreach (Control child in parent.Controls)
  50. {
  51. controls.AddRange(GetSelfAndChildrenRecursive(child));
  52. }
  53. controls.Add(parent);
  54. return controls;
  55. }
  56. internal static Color ToGrayScale(this Color originalColor)
  57. {
  58. if (originalColor.Equals(Color.Transparent))
  59. return originalColor;
  60. int grayScale = (int)((originalColor.R * .299) + (originalColor.G * .587) + (originalColor.B * .114));
  61. return Color.FromArgb(grayScale, grayScale, grayScale);
  62. }
  63. internal static string GetOS()
  64. {
  65. string os = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ProductName", "");
  66. if (os.Contains("Windows 7"))
  67. {
  68. CurrentWindowsVersion = WindowsVersion.Windows7;
  69. }
  70. if ((os.Contains("Windows 8")) || (os.Contains("Windows 8.1")))
  71. {
  72. CurrentWindowsVersion = WindowsVersion.Windows8;
  73. }
  74. if (os.Contains("Windows 10"))
  75. {
  76. CurrentWindowsVersion = WindowsVersion.Windows10;
  77. }
  78. if (os.Contains("Windows 11"))
  79. {
  80. CurrentWindowsVersion = WindowsVersion.Windows11;
  81. }
  82. if (Program.UNSAFE_MODE)
  83. {
  84. if (os.Contains("Windows Server 2008"))
  85. {
  86. CurrentWindowsVersion = WindowsVersion.Windows7;
  87. }
  88. if (os.Contains("Windows Server 2012"))
  89. {
  90. CurrentWindowsVersion = WindowsVersion.Windows8;
  91. }
  92. if (os.Contains("Windows Server 2016") || os.Contains("Windows Server 2019"))
  93. {
  94. CurrentWindowsVersion = WindowsVersion.Windows10;
  95. }
  96. }
  97. return os;
  98. }
  99. internal static string GetBitness()
  100. {
  101. string bitness = string.Empty;
  102. if (Environment.Is64BitOperatingSystem)
  103. {
  104. bitness = "You are working with 64-bit";
  105. }
  106. else
  107. {
  108. bitness = "You are working with 32-bit";
  109. }
  110. return bitness;
  111. }
  112. internal static bool IsAdmin()
  113. {
  114. return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
  115. }
  116. internal static bool IsCompatible()
  117. {
  118. bool legit;
  119. string os = GetOS();
  120. if ((os.Contains("XP")) || (os.Contains("Vista")) || os.Contains("Server 2003"))
  121. {
  122. legit = false;
  123. }
  124. else
  125. {
  126. legit = true;
  127. }
  128. return legit;
  129. }
  130. // DEPRECATED
  131. //internal static string GetEdgeDownloadFolder()
  132. //{
  133. // string current = string.Empty;
  134. // try
  135. // {
  136. // current = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge", "DownloadDirectory", DefaultEdgeDownloadFolder).ToString();
  137. // }
  138. // catch (Exception ex)
  139. // {
  140. // current = DefaultEdgeDownloadFolder;
  141. // ErrorLogger.LogError("Utilities.GetEdgeDownloadFolder", ex.Message, ex.StackTrace);
  142. // }
  143. // return current;
  144. //}
  145. // DEPRECATED
  146. //internal static void SetEdgeDownloadFolder(string path)
  147. //{
  148. // Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge", "DownloadDirectory", path, RegistryValueKind.String);
  149. //}
  150. internal static void RunBatchFile(string batchFile)
  151. {
  152. try
  153. {
  154. using (Process p = new Process())
  155. {
  156. p.StartInfo.CreateNoWindow = true;
  157. p.StartInfo.FileName = batchFile;
  158. p.StartInfo.UseShellExecute = false;
  159. p.Start();
  160. p.WaitForExit();
  161. p.Close();
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. ErrorLogger.LogError("Utilities.RunBatchFile", ex.Message, ex.StackTrace);
  167. }
  168. }
  169. internal static void ImportRegistryScript(string scriptFile)
  170. {
  171. string path = "\"" + scriptFile + "\"";
  172. Process p = new Process();
  173. try
  174. {
  175. p.StartInfo.FileName = "regedit.exe";
  176. p.StartInfo.UseShellExecute = false;
  177. p = Process.Start("regedit.exe", "/s " + path);
  178. p.WaitForExit();
  179. }
  180. catch (Exception ex)
  181. {
  182. p.Dispose();
  183. ErrorLogger.LogError("Utilities.ImportRegistryScript", ex.Message, ex.StackTrace);
  184. }
  185. finally
  186. {
  187. p.Dispose();
  188. }
  189. }
  190. internal static void Reboot()
  191. {
  192. Utilities.RunCommand("shutdown /r /t 0");
  193. }
  194. internal static void ActivateMainForm()
  195. {
  196. Program.MainForm.Activate();
  197. }
  198. internal static bool ServiceExists(string serviceName)
  199. {
  200. return ServiceController.GetServices().Any(serviceController => serviceController.ServiceName.Equals(serviceName));
  201. }
  202. internal static void StopService(string serviceName)
  203. {
  204. if (ServiceExists(serviceName))
  205. {
  206. ServiceController sc = new ServiceController(serviceName);
  207. if (sc.CanStop)
  208. {
  209. sc.Stop();
  210. }
  211. }
  212. }
  213. internal static void StartService(string serviceName)
  214. {
  215. if (ServiceExists(serviceName))
  216. {
  217. ServiceController sc = new ServiceController(serviceName);
  218. try
  219. {
  220. sc.Start();
  221. }
  222. catch (Exception ex)
  223. {
  224. ErrorLogger.LogError("Utilities.StartService", ex.Message, ex.StackTrace);
  225. }
  226. }
  227. }
  228. private static void GetRegistryStartupItemsHelper(ref List<StartupItem> list, StartupItemLocation location, StartupItemType type)
  229. {
  230. string keyPath = string.Empty;
  231. RegistryKey hive = null;
  232. if (location == StartupItemLocation.HKLM)
  233. {
  234. hive = Registry.LocalMachine;
  235. if (type == StartupItemType.Run)
  236. {
  237. keyPath = LocalMachineRun;
  238. }
  239. else if (type == StartupItemType.RunOnce)
  240. {
  241. keyPath = LocalMachineRunOnce;
  242. }
  243. }
  244. else if (location == StartupItemLocation.HKLMWoW)
  245. {
  246. hive = Registry.LocalMachine;
  247. if (type == StartupItemType.Run)
  248. {
  249. keyPath = LocalMachineRunWoW;
  250. }
  251. else if (type == StartupItemType.RunOnce)
  252. {
  253. keyPath = LocalMachineRunOnceWow;
  254. }
  255. }
  256. else if (location == StartupItemLocation.HKCU)
  257. {
  258. hive = Registry.CurrentUser;
  259. if (type == StartupItemType.Run)
  260. {
  261. keyPath = CurrentUserRun;
  262. }
  263. else if (type == StartupItemType.RunOnce)
  264. {
  265. keyPath = CurrentUserRunOnce;
  266. }
  267. }
  268. if (hive != null)
  269. {
  270. try
  271. {
  272. RegistryKey key = hive.OpenSubKey(keyPath, true);
  273. if (key != null)
  274. {
  275. string[] valueNames = key.GetValueNames();
  276. foreach (string x in valueNames)
  277. {
  278. try
  279. {
  280. RegistryStartupItem item = new RegistryStartupItem();
  281. item.Name = x;
  282. item.FileLocation = key.GetValue(x).ToString();
  283. item.Key = key;
  284. item.RegistryLocation = location;
  285. item.StartupType = type;
  286. list.Add(item);
  287. }
  288. catch (Exception ex)
  289. {
  290. ErrorLogger.LogError("Utilities.GetRegistryStartupItemsHelper", ex.Message, ex.StackTrace);
  291. }
  292. }
  293. }
  294. }
  295. catch (Exception ex)
  296. {
  297. ErrorLogger.LogError("Utilities.GetRegistryStartupItemsHelper", ex.Message, ex.StackTrace);
  298. }
  299. }
  300. }
  301. private static void GetFolderStartupItemsHelper(ref List<StartupItem> list, string[] files, string[] shortcuts)
  302. {
  303. foreach (string file in files)
  304. {
  305. try
  306. {
  307. FolderStartupItem item = new FolderStartupItem();
  308. item.Name = Path.GetFileNameWithoutExtension(file);
  309. item.FileLocation = file;
  310. item.Shortcut = file;
  311. item.RegistryLocation = StartupItemLocation.Folder;
  312. list.Add(item);
  313. }
  314. catch (Exception ex)
  315. {
  316. ErrorLogger.LogError("Utilities.GetFolderStartupItemsHelper", ex.Message, ex.StackTrace);
  317. }
  318. }
  319. foreach (string shortcut in shortcuts)
  320. {
  321. try
  322. {
  323. FolderStartupItem item = new FolderStartupItem();
  324. item.Name = Path.GetFileNameWithoutExtension(shortcut);
  325. item.FileLocation = GetShortcutTargetFile(shortcut);
  326. item.Shortcut = shortcut;
  327. item.RegistryLocation = StartupItemLocation.Folder;
  328. list.Add(item);
  329. }
  330. catch (Exception ex)
  331. {
  332. ErrorLogger.LogError("Utilities.GetFolderStartupItemsHelper", ex.Message, ex.StackTrace);
  333. }
  334. }
  335. }
  336. internal static List<StartupItem> GetStartupItems()
  337. {
  338. List<StartupItem> startupItems = new List<StartupItem>();
  339. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLM, StartupItemType.Run);
  340. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLM, StartupItemType.RunOnce);
  341. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKCU, StartupItemType.Run);
  342. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKCU, StartupItemType.RunOnce);
  343. if (Environment.Is64BitOperatingSystem)
  344. {
  345. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLMWoW, StartupItemType.Run);
  346. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLMWoW, StartupItemType.RunOnce);
  347. }
  348. if (Directory.Exists(CurrentUserStartupFolder))
  349. {
  350. string[] currentUserFiles = Directory.EnumerateFiles(CurrentUserStartupFolder, "*.*", SearchOption.AllDirectories)
  351. .Where(s => s.EndsWith(".exe") || s.EndsWith(".bat")).ToArray();
  352. string[] currentUserShortcuts = Directory.GetFiles(CurrentUserStartupFolder, "*.lnk", SearchOption.AllDirectories);
  353. GetFolderStartupItemsHelper(ref startupItems, currentUserFiles, currentUserShortcuts);
  354. }
  355. if (Directory.Exists(LocalMachineStartupFolder))
  356. {
  357. string[] localMachineFiles = Directory.EnumerateFiles(LocalMachineStartupFolder, "*.*", SearchOption.AllDirectories)
  358. .Where(s => s.EndsWith(".exe") || s.EndsWith(".bat")).ToArray();
  359. string[] localMachineShortcuts = Directory.GetFiles(LocalMachineStartupFolder, "*.lnk", SearchOption.AllDirectories);
  360. GetFolderStartupItemsHelper(ref startupItems, localMachineFiles, localMachineShortcuts);
  361. }
  362. return startupItems;
  363. }
  364. internal static void EnableFirewall()
  365. {
  366. RunCommand("netsh advfirewall set currentprofile state on");
  367. }
  368. internal static void EnableCommandPrompt()
  369. {
  370. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Policies\\Microsoft\\Windows\\System"))
  371. {
  372. key.SetValue("DisableCMD", 0, RegistryValueKind.DWord);
  373. }
  374. }
  375. internal static void EnableControlPanel()
  376. {
  377. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  378. {
  379. key.SetValue("NoControlPanel", 0, RegistryValueKind.DWord);
  380. }
  381. }
  382. internal static void EnableFolderOptions()
  383. {
  384. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  385. {
  386. key.SetValue("NoFolderOptions", 0, RegistryValueKind.DWord);
  387. }
  388. }
  389. internal static void EnableRunDialog()
  390. {
  391. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  392. {
  393. key.SetValue("NoRun", 0, RegistryValueKind.DWord);
  394. }
  395. }
  396. internal static void EnableContextMenu()
  397. {
  398. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  399. {
  400. key.SetValue("NoViewContextMenu", 0, RegistryValueKind.DWord);
  401. }
  402. }
  403. internal static void EnableTaskManager()
  404. {
  405. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"))
  406. {
  407. key.SetValue("DisableTaskMgr", 0, RegistryValueKind.DWord);
  408. }
  409. }
  410. internal static void EnableRegistryEditor()
  411. {
  412. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"))
  413. {
  414. key.SetValue("DisableRegistryTools", 0, RegistryValueKind.DWord);
  415. }
  416. }
  417. internal static void RunCommand(string command)
  418. {
  419. using (Process p = new Process())
  420. {
  421. p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  422. p.StartInfo.FileName = "cmd.exe";
  423. p.StartInfo.Arguments = "/C " + command;
  424. try
  425. {
  426. p.Start();
  427. p.WaitForExit();
  428. p.Close();
  429. }
  430. catch (Exception ex)
  431. {
  432. ErrorLogger.LogError("Utilities.RunCommand", ex.Message, ex.StackTrace);
  433. }
  434. }
  435. }
  436. internal static void FindFile(string fileName)
  437. {
  438. if (File.Exists(fileName))
  439. {
  440. Process.Start("explorer.exe", "/select, " + fileName);
  441. }
  442. }
  443. internal static string GetShortcutTargetFile(string shortcutFilename)
  444. {
  445. string pathOnly = Path.GetDirectoryName(shortcutFilename);
  446. string filenameOnly = Path.GetFileName(shortcutFilename);
  447. Shell32.Shell shell = new Shell32.Shell();
  448. Shell32.Folder folder = shell.NameSpace(pathOnly);
  449. Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
  450. if (folderItem != null)
  451. {
  452. Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
  453. return link.Path;
  454. }
  455. return string.Empty;
  456. }
  457. internal static void RestartExplorer()
  458. {
  459. const string explorer = "explorer.exe";
  460. string explorerPath = string.Format("{0}\\{1}", Environment.GetEnvironmentVariable("WINDIR"), explorer);
  461. foreach (Process process in Process.GetProcesses())
  462. {
  463. try
  464. {
  465. if (string.Compare(process.MainModule.FileName, explorerPath, StringComparison.OrdinalIgnoreCase) == 0)
  466. {
  467. process.Kill();
  468. }
  469. }
  470. catch (Exception ex)
  471. {
  472. ErrorLogger.LogError("Utilities.RestartExplorer", ex.Message, ex.StackTrace);
  473. }
  474. }
  475. Process.Start(explorer);
  476. }
  477. internal static void FindKeyInRegistry(string key)
  478. {
  479. try
  480. {
  481. Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit", "LastKey", key);
  482. Process.Start("regedit");
  483. }
  484. catch (Exception ex)
  485. {
  486. ErrorLogger.LogError("Utilities.FindKeyInRegistry", ex.Message, ex.StackTrace);
  487. }
  488. }
  489. internal static List<string> GetModernApps(bool showAll)
  490. {
  491. List<string> modernApps = new List<string>();
  492. using (PowerShell script = PowerShell.Create())
  493. {
  494. if (showAll)
  495. {
  496. script.AddScript("Get-AppxPackage -AllUsers | Select -Unique Name | Out-String -Stream");
  497. }
  498. else
  499. {
  500. script.AddScript(@"Get-AppxPackage -AllUsers | Where {$_.NonRemovable -like ""False""} | Select -Unique Name | Out-String -Stream");
  501. }
  502. string tmp = string.Empty;
  503. foreach (PSObject x in script.Invoke())
  504. {
  505. tmp = x.ToString().Trim();
  506. if (!string.IsNullOrEmpty(tmp) && !tmp.Contains("---") && !tmp.Equals("Name"))
  507. {
  508. modernApps.Add(tmp);
  509. }
  510. }
  511. }
  512. return modernApps;
  513. }
  514. internal static bool UninstallModernApp(string appName)
  515. {
  516. using (PowerShell script = PowerShell.Create())
  517. {
  518. script.AddScript(string.Format("Get-AppxPackage -AllUsers *{0}* | Remove-AppxPackage", appName));
  519. script.Invoke();
  520. return script.Streams.Error.Count > 0;
  521. // not working on Windows 7 anymore
  522. //return script.HadErrors;
  523. }
  524. }
  525. internal static void ResetConfiguration(bool withoutRestart = false)
  526. {
  527. try
  528. {
  529. Directory.Delete(Required.CoreFolder, true);
  530. }
  531. catch (Exception ex)
  532. {
  533. ErrorLogger.LogError("Utilities.ResetConfiguration", ex.Message, ex.StackTrace);
  534. }
  535. finally
  536. {
  537. if (withoutRestart == false) Application.Restart();
  538. }
  539. }
  540. internal static Task RunAsync(this Process process)
  541. {
  542. var tcs = new TaskCompletionSource<object>();
  543. process.EnableRaisingEvents = true;
  544. process.Exited += (s, e) => tcs.TrySetResult(null);
  545. if (!process.Start()) tcs.SetException(new Exception("Failed to start process."));
  546. return tcs.Task;
  547. }
  548. internal static PingReply PingHost(string nameOrAddress)
  549. {
  550. PingReply reply;
  551. try
  552. {
  553. addressToPing = Dns.GetHostAddresses(nameOrAddress)
  554. .First(address => address.AddressFamily == AddressFamily.InterNetwork);
  555. reply = pinger.Send(addressToPing);
  556. return reply;
  557. }
  558. catch
  559. {
  560. return null;
  561. }
  562. }
  563. internal static bool IsInternetAvailable()
  564. {
  565. const int timeout = 1000;
  566. const string host = "1.1.1.1";
  567. var ping = new Ping();
  568. var buffer = new byte[32];
  569. var pingOptions = new PingOptions();
  570. try
  571. {
  572. var reply = ping.Send(host, timeout, buffer, pingOptions);
  573. return (reply != null && reply.Status == IPStatus.Success);
  574. }
  575. catch (Exception)
  576. {
  577. return false;
  578. }
  579. }
  580. internal static void FlushDNSCache()
  581. {
  582. Utilities.RunBatchFile(Required.ScriptsFolder + "FlushDNSCache.bat");
  583. //Utilities.RunCommand("ipconfig /release && ipconfig /renew && arp -d * && nbtstat -R && nbtstat -RR && ipconfig /flushdns && ipconfig /registerdns");
  584. }
  585. internal static string SanitizeFileFolderName(string fileName)
  586. {
  587. char[] invalids = Path.GetInvalidFileNameChars();
  588. return string.Join("_", fileName.Split(invalids, StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');
  589. }
  590. // attempt to enable Local Group Policy Editor on Windows 10 Home editions
  591. internal static void EnableGPEDitor()
  592. {
  593. Utilities.RunBatchFile(Required.ScriptsFolder + "GPEditEnablerInHome.bat");
  594. }
  595. internal static string CheckNETFramework()
  596. {
  597. string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
  598. int netRelease;
  599. using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
  600. {
  601. if (ndpKey != null && ndpKey.GetValue("Release") != null)
  602. {
  603. netRelease = (int)ndpKey.GetValue("Release");
  604. }
  605. else
  606. {
  607. return "4.0";
  608. }
  609. }
  610. if (netRelease >= 528040)
  611. return "4.8";
  612. if (netRelease >= 461808)
  613. return "4.7.2";
  614. if (netRelease >= 461308)
  615. return "4.7.1";
  616. if (netRelease >= 460798)
  617. return "4.7";
  618. if (netRelease >= 394802)
  619. return "4.6.2";
  620. if (netRelease >= 394254)
  621. return "4.6.1";
  622. if (netRelease >= 393295)
  623. return "4.6";
  624. if (netRelease >= 379893)
  625. return "4.5.2";
  626. if (netRelease >= 378675)
  627. return "4.5.1";
  628. if (netRelease >= 378389)
  629. return "4.5";
  630. return "4.0";
  631. }
  632. }
  633. }