Utilities.cs 26 KB

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