Integrator.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. static string _newLine = Environment.NewLine;
  17. 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)";
  18. 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";
  19. 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";
  20. 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";
  21. 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";
  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 key = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + keyword;
  34. Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + keyword);
  35. Registry.SetValue(key, "", file);
  36. Registry.SetValue(key, "Path", file.Substring(0, file.LastIndexOf("\\")));
  37. }
  38. internal static List<string> GetCustomCommands()
  39. {
  40. List<string> items = new List<string>();
  41. using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\"))
  42. {
  43. foreach (string command in key.GetSubKeyNames())
  44. {
  45. items.Add(command);
  46. }
  47. }
  48. return items;
  49. }
  50. internal static void DeleteCustomCommand(string command)
  51. {
  52. Registry.LocalMachine.DeleteSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + command, false);
  53. }
  54. private static void CreateDefaultCommand(string itemName)
  55. {
  56. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + itemName, true))
  57. {
  58. key.CreateSubKey("command", RegistryKeyPermissionCheck.Default);
  59. }
  60. }
  61. internal static List<string> GetDesktopItems()
  62. {
  63. List<string> items = new List<string>();
  64. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", false))
  65. {
  66. foreach (string item in key.GetSubKeyNames())
  67. {
  68. // filter the list, so the default items will not be visible
  69. if (item.Contains("Gadgets")) continue;
  70. if (item.Contains("Display")) continue;
  71. if (item.Contains("Personalize")) continue;
  72. items.Add(item);
  73. }
  74. }
  75. return items;
  76. }
  77. internal static void RemoveItem(string name)
  78. {
  79. try
  80. {
  81. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
  82. {
  83. try
  84. {
  85. key.DeleteSubKeyTree(name, false);
  86. }
  87. catch { }
  88. }
  89. }
  90. catch { }
  91. }
  92. internal static void RemoveAllItems(List<string> items)
  93. {
  94. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
  95. {
  96. foreach (string item in items)
  97. {
  98. try
  99. {
  100. key.DeleteSubKeyTree(item, false);
  101. }
  102. catch { }
  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 { }
  132. return favicon;
  133. }
  134. internal static void AddItem(string name, string item, string icon, DesktopTypePosition position, bool shift, DesktopItemType type)
  135. {
  136. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell", true))
  137. {
  138. key.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
  139. }
  140. CreateDefaultCommand(name);
  141. if (shift)
  142. {
  143. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Extended", "");
  144. }
  145. else
  146. {
  147. using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"DesktopBackground\Shell\" + name, true))
  148. {
  149. key.CreateSubKey(name, RegistryKeyPermissionCheck.Default);
  150. }
  151. }
  152. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Icon", icon);
  153. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name, "Position", position.ToString());
  154. switch (type)
  155. {
  156. case DesktopItemType.Program:
  157. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", item);
  158. break;
  159. case DesktopItemType.Folder:
  160. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", "explorer " + item);
  161. break;
  162. case DesktopItemType.Link:
  163. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", "explorer " + item);
  164. break;
  165. case DesktopItemType.File:
  166. string tmp = @"""";
  167. string tmp2 = "explorer.exe";
  168. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", tmp2 + " " + tmp + item + tmp);
  169. break;
  170. case DesktopItemType.Command:
  171. Registry.SetValue(@"HKEY_CLASSES_ROOT\DesktopBackground\Shell\" + name + "\\command", "", item);
  172. break;
  173. }
  174. }
  175. internal static void TakeOwnership(bool remove)
  176. {
  177. if (!File.Exists(Required.ReadyMadeMenusFolder + "InstallTakeOwnership.reg"))
  178. {
  179. try
  180. {
  181. File.WriteAllText(Required.ReadyMadeMenusFolder + "InstallTakeOwnership.reg", Properties.Resources.InstallTakeOwnership);
  182. }
  183. catch { }
  184. }
  185. if (!File.Exists(Required.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg"))
  186. {
  187. try
  188. {
  189. File.WriteAllText(Required.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg", Properties.Resources.RemoveTakeOwnership);
  190. }
  191. catch { }
  192. }
  193. if (!remove)
  194. {
  195. Utilities.ImportRegistryScript(Required.ReadyMadeMenusFolder + "InstallTakeOwnership.reg");
  196. }
  197. else
  198. {
  199. Utilities.ImportRegistryScript(Required.ReadyMadeMenusFolder + "RemoveTakeOwnership.reg");
  200. }
  201. }
  202. }
  203. }