Utilities.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Security.Principal;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Diagnostics;
  12. using System.ServiceProcess;
  13. using System.Management.Automation;
  14. using System.Drawing;
  15. namespace Optimizer
  16. {
  17. internal static class Utilities
  18. {
  19. internal static readonly string LocalMachineRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
  20. internal static readonly string LocalMachineRunOnce = "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
  21. internal static readonly string LocalMachineRunWoW = "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run";
  22. internal static readonly string LocalMachineRunOnceWow = "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
  23. internal static readonly string CurrentUserRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
  24. internal static readonly string CurrentUserRunOnce = "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
  25. internal static readonly string LocalMachineStartupFolder = CleanHelper.ProgramData + "\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
  26. internal static readonly string CurrentUserStartupFolder = CleanHelper.ProfileAppDataRoaming + "\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
  27. internal readonly static string DefaultEdgeDownloadFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads");
  28. internal static WindowsVersion CurrentWindowsVersion = WindowsVersion.Unsupported;
  29. internal delegate void SetControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue);
  30. internal static void SetControlPropertyThreadSafe(Control control, string propertyName, object propertyValue)
  31. {
  32. if (control.InvokeRequired)
  33. {
  34. control.Invoke(new SetControlPropertyThreadSafeDelegate(SetControlPropertyThreadSafe), new object[] { control, propertyName, propertyValue });
  35. }
  36. else
  37. {
  38. control.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, control, new object[] { propertyValue });
  39. }
  40. }
  41. internal static IEnumerable<Control> GetSelfAndChildrenRecursive(Control parent)
  42. {
  43. List<Control> controls = new List<Control>();
  44. foreach (Control child in parent.Controls)
  45. {
  46. controls.AddRange(GetSelfAndChildrenRecursive(child));
  47. }
  48. controls.Add(parent);
  49. return controls;
  50. }
  51. internal static Color ToGrayScale(this Color originalColor)
  52. {
  53. if (originalColor.Equals(Color.Transparent))
  54. return originalColor;
  55. int grayScale = (int)((originalColor.R * .299) + (originalColor.G * .587) + (originalColor.B * .114));
  56. return Color.FromArgb(grayScale, grayScale, grayScale);
  57. }
  58. internal static string GetOS()
  59. {
  60. string os = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ProductName", "");
  61. if (os.Contains("Windows 7"))
  62. {
  63. CurrentWindowsVersion = WindowsVersion.Windows7;
  64. }
  65. if ((os.Contains("Windows 8")) || (os.Contains("Windows 8.1")))
  66. {
  67. CurrentWindowsVersion = WindowsVersion.Windows8;
  68. }
  69. if (os.Contains("Windows 10"))
  70. {
  71. CurrentWindowsVersion = WindowsVersion.Windows10;
  72. }
  73. return os;
  74. }
  75. internal static string GetBitness()
  76. {
  77. string bitness = string.Empty;
  78. if (Environment.Is64BitOperatingSystem)
  79. {
  80. bitness = "You are working with 64-bit architecture";
  81. }
  82. else
  83. {
  84. bitness = "You are working with 32-bit architecture";
  85. }
  86. return bitness;
  87. }
  88. internal static bool IsAdmin()
  89. {
  90. return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
  91. }
  92. internal static bool IsCompatible()
  93. {
  94. bool legit;
  95. string os = GetOS();
  96. if ((os.Contains("XP")) || (os.Contains("Vista")) || os.Contains("Server 2003"))
  97. {
  98. legit = false;
  99. }
  100. else
  101. {
  102. legit = true;
  103. }
  104. return legit;
  105. }
  106. internal static string GetEdgeDownloadFolder()
  107. {
  108. string current = string.Empty;
  109. try
  110. {
  111. current = Registry.GetValue(@"HKEY_CURRENT_USER\SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppContainer\Storage\microsoft.microsoftedge_8wekyb3d8bbwe\MicrosoftEdge\Main", "Default Download Directory", DefaultEdgeDownloadFolder).ToString();
  112. }
  113. catch
  114. {
  115. current = DefaultEdgeDownloadFolder;
  116. }
  117. return current;
  118. }
  119. internal static void SetEdgeDownloadFolder(string path)
  120. {
  121. Registry.SetValue(@"HKEY_CURRENT_USER\SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppContainer\Storage\microsoft.microsoftedge_8wekyb3d8bbwe\MicrosoftEdge\Main", "Default Download Directory", path, RegistryValueKind.String);
  122. }
  123. internal static void RunBatchFile(string batchFile)
  124. {
  125. try
  126. {
  127. using (Process p = new Process())
  128. {
  129. p.StartInfo.CreateNoWindow = true;
  130. p.StartInfo.FileName = batchFile;
  131. p.StartInfo.UseShellExecute = false;
  132. p.Start();
  133. p.WaitForExit();
  134. p.Close();
  135. }
  136. }
  137. catch { }
  138. }
  139. internal static void ImportRegistryScript(string scriptFile)
  140. {
  141. string path = "\"" + scriptFile + "\"";
  142. Process p = new Process();
  143. try
  144. {
  145. p.StartInfo.FileName = "regedit.exe";
  146. p.StartInfo.UseShellExecute = false;
  147. p = Process.Start("regedit.exe", "/s " + path);
  148. p.WaitForExit();
  149. }
  150. catch
  151. {
  152. p.Dispose();
  153. }
  154. finally
  155. {
  156. p.Dispose();
  157. }
  158. }
  159. internal static void Reboot()
  160. {
  161. Process.Start("shutdown", "/r /t 0");
  162. }
  163. internal static void ActivateMainForm()
  164. {
  165. Program.MainForm.Activate();
  166. }
  167. internal static bool ServiceExists(string serviceName)
  168. {
  169. return ServiceController.GetServices().Any(serviceController => serviceController.ServiceName.Equals(serviceName));
  170. }
  171. internal static void StopService(string serviceName)
  172. {
  173. if (ServiceExists(serviceName))
  174. {
  175. ServiceController sc = new ServiceController(serviceName);
  176. if (sc.CanStop)
  177. {
  178. sc.Stop();
  179. }
  180. }
  181. }
  182. internal static void StartService(string serviceName)
  183. {
  184. if (ServiceExists(serviceName))
  185. {
  186. ServiceController sc = new ServiceController(serviceName);
  187. try
  188. {
  189. sc.Start();
  190. }
  191. catch { }
  192. }
  193. }
  194. private static void GetRegistryStartupItemsHelper(ref List<StartupItem> list, StartupItemLocation location, StartupItemType type)
  195. {
  196. string keyPath = string.Empty;
  197. RegistryKey hive = null;
  198. if (location == StartupItemLocation.HKLM)
  199. {
  200. hive = Registry.LocalMachine;
  201. if (type == StartupItemType.Run)
  202. {
  203. keyPath = LocalMachineRun;
  204. }
  205. else if (type == StartupItemType.RunOnce)
  206. {
  207. keyPath = LocalMachineRunOnce;
  208. }
  209. }
  210. else if (location == StartupItemLocation.HKLMWoW)
  211. {
  212. hive = Registry.LocalMachine;
  213. if (type == StartupItemType.Run)
  214. {
  215. keyPath = LocalMachineRunWoW;
  216. }
  217. else if (type == StartupItemType.RunOnce)
  218. {
  219. keyPath = LocalMachineRunOnceWow;
  220. }
  221. }
  222. else if (location == StartupItemLocation.HKCU)
  223. {
  224. hive = Registry.CurrentUser;
  225. if (type == StartupItemType.Run)
  226. {
  227. keyPath = CurrentUserRun;
  228. }
  229. else if (type == StartupItemType.RunOnce)
  230. {
  231. keyPath = CurrentUserRunOnce;
  232. }
  233. }
  234. if (hive != null)
  235. {
  236. RegistryKey key = hive.OpenSubKey(keyPath, true);
  237. if (key != null)
  238. {
  239. string[] valueNames = key.GetValueNames();
  240. foreach (string x in valueNames)
  241. {
  242. try
  243. {
  244. RegistryStartupItem item = new RegistryStartupItem();
  245. item.Name = x;
  246. item.FileLocation = key.GetValue(x).ToString();
  247. item.Key = key;
  248. item.RegistryLocation = location;
  249. item.StartupType = type;
  250. list.Add(item);
  251. }
  252. catch { }
  253. }
  254. }
  255. }
  256. }
  257. private static void GetFolderStartupItemsHelper(ref List<StartupItem> list, string[] files, string[] shortcuts)
  258. {
  259. foreach (string file in files)
  260. {
  261. try
  262. {
  263. FolderStartupItem item = new FolderStartupItem();
  264. item.Name = Path.GetFileNameWithoutExtension(file);
  265. item.FileLocation = file;
  266. item.Shortcut = file;
  267. item.RegistryLocation = StartupItemLocation.Folder;
  268. list.Add(item);
  269. }
  270. catch { }
  271. }
  272. foreach (string shortcut in shortcuts)
  273. {
  274. try
  275. {
  276. FolderStartupItem item = new FolderStartupItem();
  277. item.Name = Path.GetFileNameWithoutExtension(shortcut);
  278. item.FileLocation = GetShortcutTargetFile(shortcut);
  279. item.Shortcut = shortcut;
  280. item.RegistryLocation = StartupItemLocation.Folder;
  281. list.Add(item);
  282. }
  283. catch { }
  284. }
  285. }
  286. internal static List<StartupItem> GetStartupItems()
  287. {
  288. List<StartupItem> startupItems = new List<StartupItem>();
  289. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLM, StartupItemType.Run);
  290. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLM, StartupItemType.RunOnce);
  291. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKCU, StartupItemType.Run);
  292. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKCU, StartupItemType.RunOnce);
  293. if (Environment.Is64BitOperatingSystem)
  294. {
  295. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLMWoW, StartupItemType.Run);
  296. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLMWoW, StartupItemType.RunOnce);
  297. }
  298. string[] currentUserFiles = Directory.GetFiles(CurrentUserStartupFolder, "*.exe", SearchOption.AllDirectories);
  299. string[] currentUserShortcuts = Directory.GetFiles(CurrentUserStartupFolder, "*.lnk", SearchOption.AllDirectories);
  300. GetFolderStartupItemsHelper(ref startupItems, currentUserFiles, currentUserShortcuts);
  301. string[] localMachineFiles = Directory.GetFiles(LocalMachineStartupFolder, "*.exe", SearchOption.AllDirectories);
  302. string[] localMachineShortcuts = Directory.GetFiles(LocalMachineStartupFolder, "*.lnk", SearchOption.AllDirectories);
  303. GetFolderStartupItemsHelper(ref startupItems, localMachineFiles, localMachineShortcuts);
  304. return startupItems;
  305. }
  306. internal static void EnableFirewall()
  307. {
  308. RunCommand("netsh advfirewall set currentprofile state on");
  309. }
  310. internal static void EnableCommandPrompt()
  311. {
  312. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Policies\\Microsoft\\Windows\\System"))
  313. {
  314. key.SetValue("DisableCMD", 0, RegistryValueKind.DWord);
  315. }
  316. }
  317. internal static void EnableControlPanel()
  318. {
  319. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  320. {
  321. key.SetValue("NoControlPanel", 0, RegistryValueKind.DWord);
  322. }
  323. }
  324. internal static void EnableFolderOptions()
  325. {
  326. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  327. {
  328. key.SetValue("NoFolderOptions", 0, RegistryValueKind.DWord);
  329. }
  330. }
  331. internal static void EnableRunDialog()
  332. {
  333. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  334. {
  335. key.SetValue("NoRun", 0, RegistryValueKind.DWord);
  336. }
  337. }
  338. internal static void EnableContextMenu()
  339. {
  340. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"))
  341. {
  342. key.SetValue("NoViewContextMenu", 0, RegistryValueKind.DWord);
  343. }
  344. }
  345. internal static void EnableTaskManager()
  346. {
  347. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"))
  348. {
  349. key.SetValue("DisableTaskMgr", 0, RegistryValueKind.DWord);
  350. }
  351. }
  352. internal static void EnableRegistryEditor()
  353. {
  354. using (RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"))
  355. {
  356. key.SetValue("DisableRegistryTools", 0, RegistryValueKind.DWord);
  357. }
  358. }
  359. internal static void RunCommand(string command)
  360. {
  361. using (Process p = new Process())
  362. {
  363. p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  364. p.StartInfo.FileName = "cmd.exe";
  365. p.StartInfo.Arguments = "/C " + command;
  366. try
  367. {
  368. p.Start();
  369. p.WaitForExit();
  370. p.Close();
  371. }
  372. catch { }
  373. }
  374. }
  375. internal static void FindFile(string fileName)
  376. {
  377. if (File.Exists(fileName))
  378. {
  379. Process.Start("explorer.exe", "/select, " + fileName);
  380. }
  381. }
  382. internal static string GetShortcutTargetFile(string shortcutFilename)
  383. {
  384. string pathOnly = Path.GetDirectoryName(shortcutFilename);
  385. string filenameOnly = Path.GetFileName(shortcutFilename);
  386. Shell32.Shell shell = new Shell32.Shell();
  387. Shell32.Folder folder = shell.NameSpace(pathOnly);
  388. Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
  389. if (folderItem != null)
  390. {
  391. Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
  392. return link.Path;
  393. }
  394. return string.Empty;
  395. }
  396. internal static void RestartExplorer()
  397. {
  398. const string explorer = "explorer.exe";
  399. string explorerPath = string.Format("{0}\\{1}", Environment.GetEnvironmentVariable("WINDIR"), explorer);
  400. foreach (Process process in Process.GetProcesses())
  401. {
  402. try
  403. {
  404. if (string.Compare(process.MainModule.FileName, explorerPath, StringComparison.OrdinalIgnoreCase) == 0)
  405. {
  406. process.Kill();
  407. }
  408. }
  409. catch { }
  410. }
  411. Process.Start(explorer);
  412. }
  413. internal static void FindKeyInRegistry(string key)
  414. {
  415. try
  416. {
  417. Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit", "LastKey", key);
  418. Process.Start("regedit");
  419. }
  420. catch { }
  421. }
  422. internal static List<string> GetModernApps()
  423. {
  424. List<string> modernApps = new List<string>();
  425. using (PowerShell script = PowerShell.Create())
  426. {
  427. script.AddScript("Get-AppxPackage | Select Name | Out-String -Stream");
  428. string tmp = string.Empty;
  429. foreach (PSObject x in script.Invoke())
  430. {
  431. tmp = x.ToString().Trim();
  432. if (!string.IsNullOrEmpty(tmp) && !tmp.Contains("---"))
  433. {
  434. if (tmp != "Name") modernApps.Add(tmp);
  435. }
  436. }
  437. }
  438. return modernApps;
  439. }
  440. internal static bool UninstallModernApp(string appName)
  441. {
  442. using (PowerShell script = PowerShell.Create())
  443. {
  444. script.AddScript(string.Format("Get-AppxPackage *{0}* | Remove-AppxPackage", appName));
  445. script.Invoke();
  446. return script.HadErrors;
  447. }
  448. }
  449. internal static void ResetConfiguration()
  450. {
  451. try
  452. {
  453. Directory.Delete(Required.CoreFolder, true);
  454. }
  455. catch { }
  456. finally
  457. {
  458. Application.Restart();
  459. }
  460. }
  461. }
  462. }