Utilities.cs 25 KB

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