Integrator.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 Integrator
  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. ErrorLogger.LogError("Integrator.RemoveItem", ex.Message, ex.StackTrace);
  81. }
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. ErrorLogger.LogError("Integrator.RemoveItem", ex.Message, ex.StackTrace);
  87. }
  88. }
  89. internal static void RemoveAllItems(List<string> items)
  90. {
  91. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
  92. {
  93. foreach (string item in items)
  94. {
  95. try
  96. {
  97. key.DeleteSubKeyTree(item, false);
  98. }
  99. catch (Exception ex)
  100. {
  101. ErrorLogger.LogError("Integrator.RemoveAllItems", ex.Message, ex.StackTrace);
  102. }
  103. }
  104. }
  105. }
  106. internal static string ExtractIconFromExecutable(string itemName, string fileName)
  107. {
  108. string iconPath = string.Empty;
  109. if (File.Exists(fileName))
  110. {
  111. Icon ico = Icon.ExtractAssociatedIcon(fileName);
  112. Clipboard.SetImage(ico.ToBitmap());
  113. Clipboard.GetImage().Save(Required.ExtractedIconsFolder + itemName + ".ico", ImageFormat.Bmp);
  114. Clipboard.Clear();
  115. iconPath = Required.ExtractedIconsFolder + itemName + ".ico";
  116. }
  117. return iconPath;
  118. }
  119. internal static string DownloadFavicon(string link, string name)
  120. {
  121. string favicon = string.Empty;
  122. try
  123. {
  124. Uri url = new Uri(link);
  125. if (url.HostNameType == UriHostNameType.Dns)
  126. {
  127. Image.FromStream(((HttpWebResponse)WebRequest.Create("http://" + url.Host + "/favicon.ico").GetResponse()).GetResponseStream()).Save(Required.FavIconsFolder + name + ".ico", ImageFormat.Bmp);
  128. favicon = Required.FavIconsFolder + name + ".ico";
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. ErrorLogger.LogError("Integrator.DownloadFavicon", ex.Message, ex.StackTrace);
  134. }
  135. return favicon;
  136. }
  137. internal static void AddItem(string name, string item, string icon, DesktopTypePosition position, bool shift, DesktopItemType type)
  138. {
  139. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
  140. {
  141. key.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
  142. }
  143. CreateDefaultCommand(name);
  144. if (shift)
  145. {
  146. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Extended", "");
  147. }
  148. else
  149. {
  150. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + name, true))
  151. {
  152. key.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
  153. }
  154. }
  155. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Icon", icon);
  156. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Position", position.ToString());
  157. switch (type)
  158. {
  159. case DesktopItemType.Program:
  160. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", item);
  161. break;
  162. case DesktopItemType.Folder:
  163. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", "explorer " + item);
  164. break;
  165. case DesktopItemType.Link:
  166. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", "explorer " + item);
  167. break;
  168. case DesktopItemType.File:
  169. string tmp = @"""";
  170. string tmp2 = "explorer.exe";
  171. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", tmp2 + " " + tmp + item + tmp);
  172. break;
  173. case DesktopItemType.Command:
  174. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", item);
  175. break;
  176. }
  177. }
  178. internal static void InstallOpenWithCMD()
  179. {
  180. Utilities.ImportRegistryScript(Required.ScriptsFolder + "AddOpenWithCMD.reg");
  181. }
  182. internal static void DeleteOpenWithCMD()
  183. {
  184. Registry.ClassesRoot.DeleteSubKeyTree(@"Directory\shell\OpenWithCMD", false);
  185. Registry.ClassesRoot.DeleteSubKeyTree(@"Directory\Background\shell\OpenWithCMD", false);
  186. Registry.ClassesRoot.DeleteSubKeyTree(@"Drive\shell\OpenWithCMD", false);
  187. }
  188. internal static void InstallTakeOwnership(bool remove)
  189. {
  190. if (!File.Exists(Required.ReadyMadeMenusFolder + "InstallTakeOwnership.reg"))
  191. {
  192. try
  193. {
  194. File.WriteAllText(Required.ReadyMadeMenusFolder + "InstallTakeOwnership.reg", Properties.Resources.InstallTakeOwnership);
  195. }
  196. catch (Exception ex)
  197. {
  198. ErrorLogger.LogError("Integrator.TakeOwnership", ex.Message, ex.StackTrace);
  199. }
  200. }
  201. if (!File.Exists(Required.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg"))
  202. {
  203. try
  204. {
  205. File.WriteAllText(Required.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg", Properties.Resources.RemoveTakeOwnership);
  206. }
  207. catch (Exception ex)
  208. {
  209. ErrorLogger.LogError("Integrator.TakeOwnership", ex.Message, ex.StackTrace);
  210. }
  211. }
  212. if (!remove)
  213. {
  214. Utilities.ImportRegistryScript(Required.ReadyMadeMenusFolder + "InstallTakeOwnership.reg");
  215. }
  216. else
  217. {
  218. Utilities.ImportRegistryScript(Required.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg");
  219. }
  220. }
  221. }
  222. }