Integrator.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 { }
  85. }
  86. }
  87. catch { }
  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 { }
  100. }
  101. }
  102. }
  103. internal static string ExtractIconFromExecutable(string itemName, string fileName)
  104. {
  105. string iconPath = string.Empty;
  106. if (File.Exists(fileName))
  107. {
  108. Icon ico = Icon.ExtractAssociatedIcon(fileName);
  109. Clipboard.SetImage(ico.ToBitmap());
  110. Clipboard.GetImage().Save(Required.ExtractedIconsFolder + itemName + ".ico", ImageFormat.Bmp);
  111. Clipboard.Clear();
  112. iconPath = Required.ExtractedIconsFolder + itemName + ".ico";
  113. }
  114. return iconPath;
  115. }
  116. internal static string DownloadFavicon(string link, string name)
  117. {
  118. string favicon = string.Empty;
  119. try
  120. {
  121. Uri url = new Uri(link);
  122. if (url.HostNameType == UriHostNameType.Dns)
  123. {
  124. Image.FromStream(((HttpWebResponse)WebRequest.Create("http://" + url.Host + "/favicon.ico").GetResponse()).GetResponseStream()).Save(Required.FavIconsFolder + name + ".ico", ImageFormat.Bmp);
  125. favicon = Required.FavIconsFolder + name + ".ico";
  126. }
  127. }
  128. catch { }
  129. return favicon;
  130. }
  131. internal static void AddItem(string name, string item, string icon, DesktopTypePosition position, bool shift, DesktopItemType type)
  132. {
  133. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
  134. {
  135. key.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
  136. }
  137. CreateDefaultCommand(name);
  138. if (shift)
  139. {
  140. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Extended", "");
  141. }
  142. else
  143. {
  144. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + name, true))
  145. {
  146. key.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
  147. }
  148. }
  149. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Icon", icon);
  150. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Position", position.ToString());
  151. switch (type)
  152. {
  153. case DesktopItemType.Program:
  154. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", item);
  155. break;
  156. case DesktopItemType.Folder:
  157. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", "explorer " + item);
  158. break;
  159. case DesktopItemType.Link:
  160. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", "explorer " + item);
  161. break;
  162. case DesktopItemType.File:
  163. string tmp = @"""";
  164. string tmp2 = "explorer.exe";
  165. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", tmp2 + " " + tmp + item + tmp);
  166. break;
  167. case DesktopItemType.Command:
  168. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", item);
  169. break;
  170. }
  171. }
  172. internal static void TakeOwnership(bool remove)
  173. {
  174. if (!File.Exists(Required.ReadyMadeMenusFolder + "InstallTakeOwnership.reg"))
  175. {
  176. try
  177. {
  178. File.WriteAllText(Required.ReadyMadeMenusFolder + "InstallTakeOwnership.reg", Properties.Resources.InstallTakeOwnership);
  179. }
  180. catch { }
  181. }
  182. if (!File.Exists(Required.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg"))
  183. {
  184. try
  185. {
  186. File.WriteAllText(Required.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg", Properties.Resources.RemoveTakeOwnership);
  187. }
  188. catch { }
  189. }
  190. if (!remove)
  191. {
  192. Utilities.ImportRegistryScript(Required.ReadyMadeMenusFolder + "InstallTakeOwnership.reg");
  193. }
  194. else
  195. {
  196. Utilities.ImportRegistryScript(Required.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg");
  197. }
  198. }
  199. }
  200. }