ClickOnceHelper.cs 7.4 KB

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