Integrator.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Microsoft.Win32;
  5. using System.Windows.Forms;
  6. using System.Drawing;
  7. using System.Net;
  8. using System.Drawing.Imaging;
  9. namespace Optimizer
  10. {
  11. public static class Integrator
  12. {
  13. static string _newLine = Environment.NewLine;
  14. internal static string powerinfo = "Power Menu contains the following:" + _newLine + _newLine + "Lock" + _newLine + "Sign out" + _newLine + "Switch User" + _newLine + "Sleep" + _newLine + "Hibernate" + _newLine + "Restart" + _newLine + "Restart with Boot Options Menu" + _newLine + "Shut down" + _newLine + "Shut down (Hybrid)";
  15. internal static string systemtoolsinfo = "System Tools Menu contains the following:" + _newLine + _newLine + "Control Panel" + _newLine + "Disk Cleanup" + _newLine + "Device Manager" + _newLine + "Event Viewer" + _newLine + "Registry Editor" + _newLine + "Security Center" + _newLine + "System Configuration" + _newLine + "Task Manager" + _newLine + "Task Scheduler" + _newLine + "Windows Update";
  16. internal static string systemshortcutsinfo = "System Shortcuts Menu contains the following:" + _newLine + _newLine + "Administrative Tools" + _newLine + "Change Date and Time" + _newLine + "Change Regional Settings" + _newLine + "Folder Options" + _newLine + "God Mode" + _newLine + "Internet Options" + _newLine + "Network Connections" + _newLine + "Power Options" + _newLine + "Programs and Features" + _newLine + "Recycle Bin" + _newLine + "Run" + _newLine + "Search" + _newLine + "Services" + _newLine + "System Properties" + _newLine + "User Accounts" + _newLine + "User Accounts Classic" + _newLine + "Window Switcher";
  17. internal static string desktopshortcutsinfo = "Desktop Shortcuts Menu contains the following:" + _newLine + _newLine + "Change Theme" + _newLine + "Change Wallpaper" + _newLine + "Change Screen Saver" + _newLine + "Change Desktop Icons" + _newLine + "Change Sound Scheme" + _newLine + "Change Mouse Pointers" + _newLine + "Change DPI Scaling" + _newLine + "Change Window Color and Appearance";
  18. internal static string windowsappsinfo = "Windows Apps Menu contains the following:" + _newLine + _newLine + "Calculator" + _newLine + "Character Map" + _newLine + "Command Prompt" + _newLine + "Disk Defragmenter" + _newLine + "Internet Explorer" + _newLine + "Notepad" + _newLine + "Paint" + _newLine + "Problem Steps Recorder" + _newLine + "Snipping Tool" + _newLine + "Sound Recorder" + _newLine + "System Restore" + _newLine + "Task Scheduler" + _newLine + "Windows Media Player" + _newLine + "Wordpad";
  19. internal static string FolderDefaultIcon = @"%systemroot%\system32\imageres.dll,-112";
  20. private static T DirectCast<T>(object o)
  21. {
  22. return (T)o;
  23. }
  24. internal static void CreateCustomCommand(string file, string keyword)
  25. {
  26. if (!keyword.EndsWith(".exe"))
  27. {
  28. keyword = keyword + ".exe";
  29. }
  30. string key = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + keyword;
  31. Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + keyword);
  32. Registry.SetValue(key, "", file);
  33. Registry.SetValue(key, "Path", file.Substring(0, file.LastIndexOf("\\")));
  34. }
  35. internal static List<string> GetCustomCommands()
  36. {
  37. List<string> items = new List<string>();
  38. using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\"))
  39. {
  40. foreach (string command in key.GetSubKeyNames())
  41. {
  42. items.Add(command);
  43. }
  44. }
  45. return items;
  46. }
  47. internal static void DeleteCustomCommand(string command)
  48. {
  49. Registry.LocalMachine.DeleteSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + command, false);
  50. }
  51. private static void CreateDefaultCommand(string itemName)
  52. {
  53. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + itemName, true))
  54. {
  55. key.CreateSubKey("command", RegistryKeyPermissionCheck.Default);
  56. }
  57. }
  58. internal static List<string> GetDesktopItems()
  59. {
  60. List<string> items = new List<string>();
  61. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", false))
  62. {
  63. foreach (string item in key.GetSubKeyNames())
  64. {
  65. // filter the list, so the default items will not be visible
  66. if (item.Contains("Gadgets")) continue;
  67. if (item.Contains("Display")) continue;
  68. if (item.Contains("Personalize")) continue;
  69. items.Add(item);
  70. }
  71. }
  72. return items;
  73. }
  74. internal static void RemoveItem(string name)
  75. {
  76. try
  77. {
  78. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
  79. {
  80. try
  81. {
  82. key.DeleteSubKeyTree(name, false);
  83. }
  84. catch (Exception ex)
  85. {
  86. ErrorLogger.LogError("Integrator.RemoveItem", ex.Message, ex.StackTrace);
  87. }
  88. }
  89. }
  90. catch (Exception ex)
  91. {
  92. ErrorLogger.LogError("Integrator.RemoveItem", ex.Message, ex.StackTrace);
  93. }
  94. }
  95. internal static void RemoveAllItems(List<string> items)
  96. {
  97. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
  98. {
  99. foreach (string item in items)
  100. {
  101. try
  102. {
  103. key.DeleteSubKeyTree(item, false);
  104. }
  105. catch (Exception ex)
  106. {
  107. ErrorLogger.LogError("Integrator.RemoveAllItems", ex.Message, ex.StackTrace);
  108. }
  109. }
  110. }
  111. }
  112. internal static string ExtractIconFromExecutable(string itemName, string fileName)
  113. {
  114. string iconPath = string.Empty;
  115. if (File.Exists(fileName))
  116. {
  117. Icon ico = Icon.ExtractAssociatedIcon(fileName);
  118. Clipboard.SetImage(ico.ToBitmap());
  119. Clipboard.GetImage().Save(Required.ExtractedIconsFolder + itemName + ".ico", ImageFormat.Bmp);
  120. Clipboard.Clear();
  121. iconPath = Required.ExtractedIconsFolder + itemName + ".ico";
  122. }
  123. return iconPath;
  124. }
  125. internal static string DownloadFavicon(string link, string name)
  126. {
  127. string favicon = string.Empty;
  128. try
  129. {
  130. Uri url = new Uri(link);
  131. if (url.HostNameType == UriHostNameType.Dns)
  132. {
  133. Image.FromStream(((HttpWebResponse)WebRequest.Create("http://" + url.Host + "/favicon.ico").GetResponse()).GetResponseStream()).Save(Required.FavIconsFolder + name + ".ico", ImageFormat.Bmp);
  134. favicon = Required.FavIconsFolder + name + ".ico";
  135. }
  136. }
  137. catch (Exception ex)
  138. {
  139. ErrorLogger.LogError("Integrator.DownloadFavicon", ex.Message, ex.StackTrace);
  140. }
  141. return favicon;
  142. }
  143. internal static void AddItem(string name, string item, string icon, DesktopTypePosition position, bool shift, DesktopItemType type)
  144. {
  145. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
  146. {
  147. key.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
  148. }
  149. CreateDefaultCommand(name);
  150. if (shift)
  151. {
  152. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Extended", "");
  153. }
  154. else
  155. {
  156. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + name, true))
  157. {
  158. key.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
  159. }
  160. }
  161. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Icon", icon);
  162. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Position", position.ToString());
  163. switch (type)
  164. {
  165. case DesktopItemType.Program:
  166. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", item);
  167. break;
  168. case DesktopItemType.Folder:
  169. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", "explorer " + item);
  170. break;
  171. case DesktopItemType.Link:
  172. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", "explorer " + item);
  173. break;
  174. case DesktopItemType.File:
  175. string tmp = @"""";
  176. string tmp2 = "explorer.exe";
  177. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", tmp2 + " " + tmp + item + tmp);
  178. break;
  179. case DesktopItemType.Command:
  180. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", item);
  181. break;
  182. }
  183. }
  184. internal static void TakeOwnership(bool remove)
  185. {
  186. if (!File.Exists(Required.ReadyMadeMenusFolder + "InstallTakeOwnership.reg"))
  187. {
  188. try
  189. {
  190. File.WriteAllText(Required.ReadyMadeMenusFolder + "InstallTakeOwnership.reg", Properties.Resources.InstallTakeOwnership);
  191. }
  192. catch (Exception ex)
  193. {
  194. ErrorLogger.LogError("Integrator.TakeOwnership", ex.Message, ex.StackTrace);
  195. }
  196. }
  197. if (!File.Exists(Required.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg"))
  198. {
  199. try
  200. {
  201. File.WriteAllText(Required.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg", Properties.Resources.RemoveTakeOwnership);
  202. }
  203. catch (Exception ex)
  204. {
  205. ErrorLogger.LogError("Integrator.TakeOwnership", ex.Message, ex.StackTrace);
  206. }
  207. }
  208. if (!remove)
  209. {
  210. Utilities.ImportRegistryScript(Required.ReadyMadeMenusFolder + "InstallTakeOwnership.reg");
  211. }
  212. else
  213. {
  214. Utilities.ImportRegistryScript(Required.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg");
  215. }
  216. }
  217. }
  218. }