ClickOnceHelper.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System.Deployment.Application;
  2. using Microsoft.Win32;
  3. using System;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Security.AccessControl;
  9. namespace MediaBrowser.ClickOnce
  10. {
  11. /// <summary>
  12. /// Class ClickOnceHelper
  13. /// </summary>
  14. public class ClickOnceHelper
  15. {
  16. /// <summary>
  17. /// The uninstall string
  18. /// </summary>
  19. private const string UninstallString = "UninstallString";
  20. /// <summary>
  21. /// The display name key
  22. /// </summary>
  23. private const string DisplayNameKey = "DisplayName";
  24. /// <summary>
  25. /// The uninstall string file
  26. /// </summary>
  27. private const string UninstallStringFile = "UninstallString.bat";
  28. /// <summary>
  29. /// The appref extension
  30. /// </summary>
  31. private const string ApprefExtension = ".appref-ms";
  32. /// <summary>
  33. /// The uninstall registry key
  34. /// </summary>
  35. private readonly RegistryKey UninstallRegistryKey;
  36. /// <summary>
  37. /// Gets the location.
  38. /// </summary>
  39. /// <value>The location.</value>
  40. private static string Location
  41. {
  42. get { return Assembly.GetExecutingAssembly().Location; }
  43. }
  44. /// <summary>
  45. /// Gets a value indicating whether this instance is network deployed.
  46. /// </summary>
  47. /// <value><c>true</c> if this instance is network deployed; otherwise, <c>false</c>.</value>
  48. public static bool IsNetworkDeployed
  49. {
  50. get { return ApplicationDeployment.IsNetworkDeployed; }
  51. }
  52. /// <summary>
  53. /// Gets the name of the publisher.
  54. /// </summary>
  55. /// <value>The name of the publisher.</value>
  56. public string PublisherName { get; private set; }
  57. /// <summary>
  58. /// Gets the name of the product.
  59. /// </summary>
  60. /// <value>The name of the product.</value>
  61. public string ProductName { get; private set; }
  62. /// <summary>
  63. /// Gets the uninstall file.
  64. /// </summary>
  65. /// <value>The uninstall file.</value>
  66. public string UninstallFile { get; private set; }
  67. /// <summary>
  68. /// Gets the name of the suite.
  69. /// </summary>
  70. /// <value>The name of the suite.</value>
  71. public string SuiteName { get; private set; }
  72. /// <summary>
  73. /// Initializes a new instance of the <see cref="ClickOnceHelper" /> class.
  74. /// </summary>
  75. /// <param name="publisherName">Name of the publisher.</param>
  76. /// <param name="productName">Name of the product.</param>
  77. /// <param name="suiteName">Name of the suite.</param>
  78. public ClickOnceHelper(string publisherName, string productName, string suiteName)
  79. {
  80. PublisherName = publisherName;
  81. ProductName = productName;
  82. SuiteName = suiteName;
  83. var publisherFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), PublisherName);
  84. if (!Directory.Exists(publisherFolder))
  85. {
  86. Directory.CreateDirectory(publisherFolder);
  87. }
  88. UninstallFile = Path.Combine(publisherFolder, UninstallStringFile);
  89. UninstallRegistryKey = GetUninstallRegistryKeyByProductName(ProductName);
  90. }
  91. /// <summary>
  92. /// Gets the shortcut path.
  93. /// </summary>
  94. /// <returns>System.String.</returns>
  95. private string GetShortcutPath()
  96. {
  97. var allProgramsPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
  98. var shortcutPath = Path.Combine(allProgramsPath, PublisherName);
  99. if (!string.IsNullOrEmpty(SuiteName))
  100. {
  101. shortcutPath = Path.Combine(shortcutPath, SuiteName);
  102. }
  103. return Path.Combine(shortcutPath, ProductName) + ApprefExtension;
  104. }
  105. /// <summary>
  106. /// Gets the startup shortcut path.
  107. /// </summary>
  108. /// <returns>System.String.</returns>
  109. private string GetStartupShortcutPath()
  110. {
  111. var startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
  112. return Path.Combine(startupPath, ProductName) + ApprefExtension;
  113. }
  114. /// <summary>
  115. /// Adds the shortcut to startup.
  116. /// </summary>
  117. public void AddShortcutToStartup()
  118. {
  119. var startupPath = GetStartupShortcutPath();
  120. if (!File.Exists(startupPath))
  121. {
  122. File.Copy(GetShortcutPath(), startupPath);
  123. }
  124. }
  125. /// <summary>
  126. /// Removes the shortcut from startup.
  127. /// </summary>
  128. public void RemoveShortcutFromStartup()
  129. {
  130. var startupPath = GetStartupShortcutPath();
  131. if (File.Exists(startupPath))
  132. {
  133. File.Delete(startupPath);
  134. }
  135. }
  136. /// <summary>
  137. /// Updates the uninstall parameters.
  138. /// </summary>
  139. /// <param name="uninstallerFileName">Name of the uninstaller file.</param>
  140. public void UpdateUninstallParameters(string uninstallerFileName)
  141. {
  142. if (UninstallRegistryKey != null)
  143. {
  144. UpdateUninstallString(uninstallerFileName);
  145. }
  146. }
  147. /// <summary>
  148. /// Gets the name of the uninstall registry key by product.
  149. /// </summary>
  150. /// <param name="productName">Name of the product.</param>
  151. /// <returns>RegistryKey.</returns>
  152. private RegistryKey GetUninstallRegistryKeyByProductName(string productName)
  153. {
  154. var subKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall");
  155. if (subKey == null)
  156. {
  157. return null;
  158. }
  159. return subKey.GetSubKeyNames()
  160. .Select(name => subKey.OpenSubKey(name, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.QueryValues | RegistryRights.ReadKey | RegistryRights.SetValue))
  161. .Where(application => application != null)
  162. .FirstOrDefault(application => application.GetValueNames().Where(appKey => appKey.Equals(DisplayNameKey)).Any(appKey => application.GetValue(appKey).Equals(productName)));
  163. }
  164. /// <summary>
  165. /// Updates the uninstall string.
  166. /// </summary>
  167. /// <param name="uninstallerFileName">Name of the uninstaller file.</param>
  168. private void UpdateUninstallString(string uninstallerFileName)
  169. {
  170. var uninstallString = (string)UninstallRegistryKey.GetValue(UninstallString);
  171. if (!string.IsNullOrEmpty(UninstallFile) && uninstallString.StartsWith("rundll32.exe", StringComparison.OrdinalIgnoreCase))
  172. {
  173. File.WriteAllText(UninstallFile, uninstallString);
  174. }
  175. var str = String.Format("\"{0}\" uninstall", Path.Combine(Path.GetDirectoryName(Location), uninstallerFileName));
  176. UninstallRegistryKey.SetValue(UninstallString, str);
  177. }
  178. /// <summary>
  179. /// Uninstalls this instance.
  180. /// </summary>
  181. public void Uninstall()
  182. {
  183. RemoveShortcutFromStartup();
  184. var uninstallString = File.ReadAllText(UninstallFile);
  185. new Process
  186. {
  187. StartInfo =
  188. {
  189. Arguments = uninstallString.Substring(uninstallString.IndexOf(" ", StringComparison.OrdinalIgnoreCase) + 1),
  190. FileName = uninstallString.Substring(0, uninstallString.IndexOf(" ", StringComparison.OrdinalIgnoreCase)),
  191. UseShellExecute = false
  192. }
  193. }.Start();
  194. }
  195. /// <summary>
  196. /// Configures the click once startup.
  197. /// </summary>
  198. /// <param name="publisherName">Name of the publisher.</param>
  199. /// <param name="productName">Name of the product.</param>
  200. /// <param name="suiteName">Name of the suite.</param>
  201. /// <param name="runAtStartup">if set to <c>true</c> [run at startup].</param>
  202. /// <param name="uninstallerFilename">The uninstaller filename.</param>
  203. public static void ConfigureClickOnceStartupIfInstalled(string publisherName, string productName, string suiteName, bool runAtStartup, string uninstallerFilename)
  204. {
  205. if (!ApplicationDeployment.IsNetworkDeployed)
  206. {
  207. return;
  208. }
  209. var clickOnceHelper = new ClickOnceHelper(publisherName, productName, suiteName);
  210. if (runAtStartup)
  211. {
  212. clickOnceHelper.UpdateUninstallParameters(uninstallerFilename);
  213. clickOnceHelper.AddShortcutToStartup();
  214. }
  215. else
  216. {
  217. clickOnceHelper.RemoveShortcutFromStartup();
  218. }
  219. }
  220. }
  221. }