IntegratorHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Net;
  8. using System.Runtime.InteropServices;
  9. using System.Windows.Forms;
  10. namespace Optimizer
  11. {
  12. public static class IntegratorHelper
  13. {
  14. internal static string FolderDefaultIcon = @"%systemroot%\system32\imageres.dll,-112";
  15. internal static void CreateCustomCommand(string file, string keyword)
  16. {
  17. if (!keyword.EndsWith(".exe"))
  18. {
  19. keyword = keyword + ".exe";
  20. }
  21. string key = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + keyword;
  22. Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + keyword);
  23. Registry.SetValue(key, "", file);
  24. Registry.SetValue(key, "Path", file.Substring(0, file.LastIndexOf("\\")));
  25. }
  26. internal static List<string> GetCustomCommands()
  27. {
  28. List<string> items = new List<string>();
  29. using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\"))
  30. {
  31. foreach (string command in key.GetSubKeyNames())
  32. {
  33. items.Add(command);
  34. }
  35. }
  36. return items;
  37. }
  38. internal static void DeleteCustomCommand(string command)
  39. {
  40. Registry.LocalMachine.DeleteSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + command, false);
  41. }
  42. private static void CreateDefaultCommand(string itemName)
  43. {
  44. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + itemName, true))
  45. {
  46. key.CreateSubKey("command", RegistryKeyPermissionCheck.Default);
  47. }
  48. }
  49. internal static List<string> GetDesktopItems()
  50. {
  51. List<string> items = new List<string>();
  52. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", false))
  53. {
  54. foreach (string item in key.GetSubKeyNames())
  55. {
  56. // filter the list, so the default items will not be visible
  57. if (item.Contains("Gadgets")) continue;
  58. if (item.Contains("Display")) continue;
  59. if (item.Contains("Personalize")) continue;
  60. items.Add(item);
  61. }
  62. }
  63. return items;
  64. }
  65. internal static void RemoveItem(string name)
  66. {
  67. try
  68. {
  69. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
  70. {
  71. try
  72. {
  73. key.DeleteSubKeyTree(name, false);
  74. }
  75. catch (Exception ex)
  76. {
  77. Logger.LogError("Integrator.RemoveItem", ex.Message, ex.StackTrace);
  78. }
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. Logger.LogError("Integrator.RemoveItem", ex.Message, ex.StackTrace);
  84. }
  85. }
  86. internal static bool DesktopItemExists(string name)
  87. {
  88. try
  89. {
  90. return Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + name, false) != null;
  91. }
  92. catch (Exception ex)
  93. {
  94. Logger.LogError("Integrator.ItemExists", ex.Message, ex.StackTrace);
  95. return false;
  96. }
  97. }
  98. internal static bool TakeOwnershipExists()
  99. {
  100. try
  101. {
  102. return Registry.ClassesRoot.OpenSubKey(@"*\shell\runas", false).GetValue("").ToString() == "Take Ownership";
  103. }
  104. catch (Exception ex)
  105. {
  106. Logger.LogError("Integrator.TakeOwnershipExists", ex.Message, ex.StackTrace);
  107. return false;
  108. }
  109. }
  110. internal static bool OpenWithCMDExists()
  111. {
  112. try
  113. {
  114. return Registry.ClassesRoot.OpenSubKey(@"Directory\shell\OpenWithCMD", false) != null;
  115. }
  116. catch (Exception ex)
  117. {
  118. Logger.LogError("Integrator.OpenWithCMDExists", ex.Message, ex.StackTrace);
  119. return false;
  120. }
  121. }
  122. internal static void RemoveAllItems(List<string> items)
  123. {
  124. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
  125. {
  126. foreach (string item in items)
  127. {
  128. try
  129. {
  130. key.DeleteSubKeyTree(item, false);
  131. }
  132. catch (Exception ex)
  133. {
  134. Logger.LogError("Integrator.RemoveAllItems", ex.Message, ex.StackTrace);
  135. }
  136. }
  137. }
  138. }
  139. internal static string ExtractIconFromExecutable(string itemName, string fileName)
  140. {
  141. string iconPath = string.Empty;
  142. if (File.Exists(fileName))
  143. {
  144. Icon ico = Icon.ExtractAssociatedIcon(fileName);
  145. Clipboard.SetImage(ico.ToBitmap());
  146. Clipboard.GetImage().Save(CoreHelper.ExtractedIconsFolder + itemName + ".ico", ImageFormat.Bmp);
  147. Clipboard.Clear();
  148. iconPath = CoreHelper.ExtractedIconsFolder + itemName + ".ico";
  149. }
  150. return iconPath;
  151. }
  152. internal static string DownloadFavicon(string link, string name)
  153. {
  154. string favicon = string.Empty;
  155. try
  156. {
  157. Uri url = new Uri(link);
  158. if (url.HostNameType == UriHostNameType.Dns)
  159. {
  160. Image.FromStream(((HttpWebResponse)WebRequest.Create("http://" + url.Host + "/favicon.ico").GetResponse()).GetResponseStream()).Save(CoreHelper.FavIconsFolder + name + ".ico", ImageFormat.Bmp);
  161. favicon = CoreHelper.FavIconsFolder + name + ".ico";
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. Logger.LogError("Integrator.DownloadFavicon", ex.Message, ex.StackTrace);
  167. }
  168. return favicon;
  169. }
  170. internal static void AddItem(string name, string item, string icon, DesktopTypePosition position, bool shift, DesktopItemType type)
  171. {
  172. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
  173. {
  174. key.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
  175. }
  176. CreateDefaultCommand(name);
  177. if (shift)
  178. {
  179. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Extended", "");
  180. }
  181. else
  182. {
  183. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + name, true))
  184. {
  185. key.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
  186. }
  187. }
  188. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Icon", icon);
  189. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Position", position.ToString());
  190. switch (type)
  191. {
  192. case DesktopItemType.Program:
  193. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", item);
  194. break;
  195. case DesktopItemType.Folder:
  196. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", "explorer " + item);
  197. break;
  198. case DesktopItemType.Link:
  199. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", "explorer " + item);
  200. break;
  201. case DesktopItemType.File:
  202. string tmp = @"""";
  203. string tmp2 = "explorer.exe";
  204. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", tmp2 + " " + tmp + item + tmp);
  205. break;
  206. case DesktopItemType.Command:
  207. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", item);
  208. break;
  209. }
  210. }
  211. internal static void InstallOpenWithCMD()
  212. {
  213. Utilities.ImportRegistryScript(CoreHelper.ScriptsFolder + "AddOpenWithCMD.reg");
  214. }
  215. internal static void DeleteOpenWithCMD()
  216. {
  217. Registry.ClassesRoot.DeleteSubKeyTree(@"Directory\shell\OpenWithCMD", false);
  218. Registry.ClassesRoot.DeleteSubKeyTree(@"Directory\Background\shell\OpenWithCMD", false);
  219. Registry.ClassesRoot.DeleteSubKeyTree(@"Drive\shell\OpenWithCMD", false);
  220. }
  221. internal static void InstallTakeOwnership(bool remove)
  222. {
  223. if (!File.Exists(CoreHelper.ReadyMadeMenusFolder + "InstallTakeOwnership.reg"))
  224. {
  225. try
  226. {
  227. File.WriteAllText(CoreHelper.ReadyMadeMenusFolder + "InstallTakeOwnership.reg", Properties.Resources.InstallTakeOwnership);
  228. }
  229. catch (Exception ex)
  230. {
  231. Logger.LogError("Integrator.TakeOwnership", ex.Message, ex.StackTrace);
  232. }
  233. }
  234. if (!File.Exists(CoreHelper.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg"))
  235. {
  236. try
  237. {
  238. File.WriteAllText(CoreHelper.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg", Properties.Resources.RemoveTakeOwnership);
  239. }
  240. catch (Exception ex)
  241. {
  242. Logger.LogError("Integrator.TakeOwnership", ex.Message, ex.StackTrace);
  243. }
  244. }
  245. if (!remove)
  246. {
  247. Utilities.ImportRegistryScript(CoreHelper.ReadyMadeMenusFolder + "InstallTakeOwnership.reg");
  248. }
  249. else
  250. {
  251. Utilities.ImportRegistryScript(CoreHelper.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg");
  252. }
  253. }
  254. /// <summary>
  255. /// PATH System Variables functions
  256. /// </summary>
  257. const int HWND_BROADCAST = 0xffff;
  258. const uint WM_SETTINGCHANGE = 0x001a;
  259. [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  260. static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam);
  261. internal static string[] GetPathSystemVariables()
  262. {
  263. try
  264. {
  265. string basePathKey = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment";
  266. using (var key = Registry.LocalMachine.OpenSubKey(basePathKey, false))
  267. {
  268. string result = key.GetValue("Path", new string[] { }).ToString();
  269. return result.Split(';');
  270. }
  271. }
  272. catch (Exception ex)
  273. {
  274. Logger.LogError("Integrator.GetPathSystemVariables", ex.Message, ex.StackTrace);
  275. return new string[] { };
  276. }
  277. }
  278. internal static void UpdatePathSystemVariables(string[] newValues)
  279. {
  280. if (newValues == null || newValues.Length <= 0)
  281. {
  282. return;
  283. }
  284. try
  285. {
  286. string basePathKey = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment";
  287. using (var key = Registry.LocalMachine.OpenSubKey(basePathKey, true))
  288. {
  289. string updatedSystemVariables = string.Join(";", newValues);
  290. key.SetValue("Path", updatedSystemVariables, RegistryValueKind.ExpandString);
  291. }
  292. }
  293. catch (Exception ex)
  294. {
  295. Logger.LogError("Integrator.UpdatePathSystemVariables", ex.Message, ex.StackTrace);
  296. }
  297. }
  298. // Notifies the shell that System variables have been changed
  299. // Otherwise, a restart is needed
  300. internal static void ApplyPathSystemVariables()
  301. {
  302. SendNotifyMessage((IntPtr)HWND_BROADCAST, WM_SETTINGCHANGE, (UIntPtr)0, "Environment");
  303. }
  304. }
  305. }