Integrator.cs 14 KB

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