IntegratorHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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.Windows.Forms;
  9. namespace Optimizer
  10. {
  11. public static class IntegratorHelper
  12. {
  13. internal static string FolderDefaultIcon = @"%systemroot%\system32\imageres.dll,-112";
  14. private static T DirectCast<T>(object o)
  15. {
  16. return (T)o;
  17. }
  18. internal static void CreateCustomCommand(string file, string keyword)
  19. {
  20. if (!keyword.EndsWith(".exe"))
  21. {
  22. keyword = keyword + ".exe";
  23. }
  24. string key = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + keyword;
  25. Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + keyword);
  26. Registry.SetValue(key, "", file);
  27. Registry.SetValue(key, "Path", file.Substring(0, file.LastIndexOf("\\")));
  28. }
  29. internal static List<string> GetCustomCommands()
  30. {
  31. List<string> items = new List<string>();
  32. using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\"))
  33. {
  34. foreach (string command in key.GetSubKeyNames())
  35. {
  36. items.Add(command);
  37. }
  38. }
  39. return items;
  40. }
  41. internal static void DeleteCustomCommand(string command)
  42. {
  43. Registry.LocalMachine.DeleteSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + command, false);
  44. }
  45. private static void CreateDefaultCommand(string itemName)
  46. {
  47. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + itemName, true))
  48. {
  49. key.CreateSubKey("command", RegistryKeyPermissionCheck.Default);
  50. }
  51. }
  52. internal static List<string> GetDesktopItems()
  53. {
  54. List<string> items = new List<string>();
  55. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", false))
  56. {
  57. foreach (string item in key.GetSubKeyNames())
  58. {
  59. // filter the list, so the default items will not be visible
  60. if (item.Contains("Gadgets")) continue;
  61. if (item.Contains("Display")) continue;
  62. if (item.Contains("Personalize")) continue;
  63. items.Add(item);
  64. }
  65. }
  66. return items;
  67. }
  68. internal static void RemoveItem(string name)
  69. {
  70. try
  71. {
  72. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
  73. {
  74. try
  75. {
  76. key.DeleteSubKeyTree(name, false);
  77. }
  78. catch (Exception ex)
  79. {
  80. Logger.LogError("Integrator.RemoveItem", ex.Message, ex.StackTrace);
  81. }
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. Logger.LogError("Integrator.RemoveItem", ex.Message, ex.StackTrace);
  87. }
  88. }
  89. internal static bool DesktopItemExists(string name)
  90. {
  91. try
  92. {
  93. return Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + name, false) != null;
  94. }
  95. catch (Exception ex)
  96. {
  97. Logger.LogError("Integrator.ItemExists", ex.Message, ex.StackTrace);
  98. return false;
  99. }
  100. }
  101. internal static bool TakeOwnershipExists()
  102. {
  103. try
  104. {
  105. return Registry.ClassesRoot.OpenSubKey(@"*\shell\runas", false).GetValue("").ToString() == "Take Ownership";
  106. }
  107. catch (Exception ex)
  108. {
  109. Logger.LogError("Integrator.TakeOwnershipExists", ex.Message, ex.StackTrace);
  110. return false;
  111. }
  112. }
  113. internal static bool OpenWithCMDExists()
  114. {
  115. try
  116. {
  117. return Registry.ClassesRoot.OpenSubKey(@"Directory\shell\OpenWithCMD", false) != null;
  118. }
  119. catch (Exception ex)
  120. {
  121. Logger.LogError("Integrator.OpenWithCMDExists", ex.Message, ex.StackTrace);
  122. return false;
  123. }
  124. }
  125. internal static void RemoveAllItems(List<string> items)
  126. {
  127. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
  128. {
  129. foreach (string item in items)
  130. {
  131. try
  132. {
  133. key.DeleteSubKeyTree(item, false);
  134. }
  135. catch (Exception ex)
  136. {
  137. Logger.LogError("Integrator.RemoveAllItems", ex.Message, ex.StackTrace);
  138. }
  139. }
  140. }
  141. }
  142. internal static string ExtractIconFromExecutable(string itemName, string fileName)
  143. {
  144. string iconPath = string.Empty;
  145. if (File.Exists(fileName))
  146. {
  147. Icon ico = Icon.ExtractAssociatedIcon(fileName);
  148. Clipboard.SetImage(ico.ToBitmap());
  149. Clipboard.GetImage().Save(CoreHelper.ExtractedIconsFolder + itemName + ".ico", ImageFormat.Bmp);
  150. Clipboard.Clear();
  151. iconPath = CoreHelper.ExtractedIconsFolder + itemName + ".ico";
  152. }
  153. return iconPath;
  154. }
  155. internal static string DownloadFavicon(string link, string name)
  156. {
  157. string favicon = string.Empty;
  158. try
  159. {
  160. Uri url = new Uri(link);
  161. if (url.HostNameType == UriHostNameType.Dns)
  162. {
  163. Image.FromStream(((HttpWebResponse)WebRequest.Create("http://" + url.Host + "/favicon.ico").GetResponse()).GetResponseStream()).Save(CoreHelper.FavIconsFolder + name + ".ico", ImageFormat.Bmp);
  164. favicon = CoreHelper.FavIconsFolder + name + ".ico";
  165. }
  166. }
  167. catch (Exception ex)
  168. {
  169. Logger.LogError("Integrator.DownloadFavicon", ex.Message, ex.StackTrace);
  170. }
  171. return favicon;
  172. }
  173. internal static void AddItem(string name, string item, string icon, DesktopTypePosition position, bool shift, DesktopItemType type)
  174. {
  175. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
  176. {
  177. key.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
  178. }
  179. CreateDefaultCommand(name);
  180. if (shift)
  181. {
  182. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Extended", "");
  183. }
  184. else
  185. {
  186. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + name, true))
  187. {
  188. key.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
  189. }
  190. }
  191. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Icon", icon);
  192. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Position", position.ToString());
  193. switch (type)
  194. {
  195. case DesktopItemType.Program:
  196. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", item);
  197. break;
  198. case DesktopItemType.Folder:
  199. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", "explorer " + item);
  200. break;
  201. case DesktopItemType.Link:
  202. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", "explorer " + item);
  203. break;
  204. case DesktopItemType.File:
  205. string tmp = @"""";
  206. string tmp2 = "explorer.exe";
  207. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", tmp2 + " " + tmp + item + tmp);
  208. break;
  209. case DesktopItemType.Command:
  210. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", item);
  211. break;
  212. }
  213. }
  214. internal static void InstallOpenWithCMD()
  215. {
  216. Utilities.ImportRegistryScript(CoreHelper.ScriptsFolder + "AddOpenWithCMD.reg");
  217. }
  218. internal static void DeleteOpenWithCMD()
  219. {
  220. Registry.ClassesRoot.DeleteSubKeyTree(@"Directory\shell\OpenWithCMD", false);
  221. Registry.ClassesRoot.DeleteSubKeyTree(@"Directory\Background\shell\OpenWithCMD", false);
  222. Registry.ClassesRoot.DeleteSubKeyTree(@"Drive\shell\OpenWithCMD", false);
  223. }
  224. internal static void InstallTakeOwnership(bool remove)
  225. {
  226. if (!File.Exists(CoreHelper.ReadyMadeMenusFolder + "InstallTakeOwnership.reg"))
  227. {
  228. try
  229. {
  230. File.WriteAllText(CoreHelper.ReadyMadeMenusFolder + "InstallTakeOwnership.reg", Properties.Resources.InstallTakeOwnership);
  231. }
  232. catch (Exception ex)
  233. {
  234. Logger.LogError("Integrator.TakeOwnership", ex.Message, ex.StackTrace);
  235. }
  236. }
  237. if (!File.Exists(CoreHelper.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg"))
  238. {
  239. try
  240. {
  241. File.WriteAllText(CoreHelper.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg", Properties.Resources.RemoveTakeOwnership);
  242. }
  243. catch (Exception ex)
  244. {
  245. Logger.LogError("Integrator.TakeOwnership", ex.Message, ex.StackTrace);
  246. }
  247. }
  248. if (!remove)
  249. {
  250. Utilities.ImportRegistryScript(CoreHelper.ReadyMadeMenusFolder + "InstallTakeOwnership.reg");
  251. }
  252. else
  253. {
  254. Utilities.ImportRegistryScript(CoreHelper.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg");
  255. }
  256. }
  257. }
  258. }