IntegratorHelper.cs 9.7 KB

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