Utilities.cs 17 KB

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