MainWindow.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using System;
  2. using System.Diagnostics;
  3. using System.Net;
  4. using System.Reflection;
  5. using Microsoft.Win32;
  6. using System.IO;
  7. using System.Threading;
  8. using System.Windows;
  9. namespace MediaBrowser.Uninstaller.Execute
  10. {
  11. /// <summary>
  12. /// Interaction logic for MainWindow.xaml
  13. /// </summary>
  14. public partial class MainWindow : Window
  15. {
  16. protected string Product = "Server";
  17. protected string RootSuffix = "-Server";
  18. public MainWindow()
  19. {
  20. var args = Environment.GetCommandLineArgs();
  21. var product = args.Length > 1 ? args[1] : null;
  22. if (product == null)
  23. {
  24. MessageBox.Show("Please use 'Programs and Features' to uninstall.");
  25. Close();
  26. }
  27. else
  28. {
  29. var callerId = args.Length > 2 ? args[2] : null;
  30. if (callerId != null)
  31. {
  32. // Wait for our caller to exit
  33. try
  34. {
  35. var process = Process.GetProcessById(Convert.ToInt32(callerId));
  36. process.WaitForExit();
  37. }
  38. catch (ArgumentException)
  39. {
  40. // wasn't running
  41. }
  42. }
  43. else
  44. {
  45. // No caller - means we were called directly and need to move to temp file and execute there
  46. //copy the real program to a temp location so we can delete everything here (including us)
  47. var us = Assembly.GetExecutingAssembly().Location;
  48. var tempExe = Path.Combine(Path.GetTempPath(), Path.GetFileName(us) ?? "Mediabrowser.Uninstaller.exe");
  49. File.Copy(us,tempExe,true);
  50. //get our pid to pass to the uninstaller so it can wait for us to exit
  51. var pid = Process.GetCurrentProcess().Id;
  52. //kick off the copy
  53. Process.Start(tempExe, product + " " + pid);
  54. //and shut down
  55. Close();
  56. }
  57. InitializeComponent();
  58. switch (product)
  59. {
  60. case "server":
  61. Product = "Server";
  62. RootSuffix = "-Server";
  63. break;
  64. case "mbt":
  65. Product = "Theater";
  66. RootSuffix = "-Theater";
  67. break;
  68. default:
  69. MessageBox.Show("Please Use 'Programs and Features' to uninstall.");
  70. Close();
  71. break;
  72. }
  73. lblHeading.Content = this.Title = "Uninstall Media Browser " + Product;
  74. }
  75. }
  76. private void btnCancel_Click(object sender, RoutedEventArgs e)
  77. {
  78. Close();
  79. }
  80. private void cbxRemoveAll_Checked(object sender, RoutedEventArgs e)
  81. {
  82. if (cbxRemoveAll.IsChecked == true)
  83. {
  84. cbxRemoveCache.IsChecked = cbxRemoveConfig.IsChecked = cbxRemovePlugins.IsChecked = true;
  85. }
  86. cbxRemoveCache.IsEnabled = cbxRemoveConfig.IsEnabled = cbxRemovePlugins.IsEnabled = !cbxRemoveAll.IsChecked.Value;
  87. }
  88. private void btnUninstall_Click(object sender, RoutedEventArgs e)
  89. {
  90. // First remove our shortcuts
  91. lblHeading.Content = "Removing Shortcuts...";
  92. btnCancel.IsEnabled = btnUninstall.IsEnabled = false;
  93. grdOptions.Visibility = Visibility.Hidden;
  94. var startMenu = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "Media Browser 3");
  95. var linkName = "Media Browser " + Product + ".lnk";
  96. RemoveShortcut(Path.Combine(startMenu, linkName));
  97. RemoveShortcut(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup),linkName));
  98. linkName = "Uninstall " + linkName;
  99. RemoveShortcut(Path.Combine(startMenu, linkName));
  100. if (Product == "Server")
  101. {
  102. RemoveShortcut(Path.Combine(startMenu, "MB Dashboard.lnk"));
  103. var procs = Process.GetProcessesByName("mediabrowser.serverapplication");
  104. var server = procs.Length > 0 ? procs[0] : null;
  105. if (server != null)
  106. {
  107. using (var client = new WebClient())
  108. {
  109. lblHeading.Content = "Shutting Down Media Browser Server...";
  110. try
  111. {
  112. client.UploadString("http://localhost:8096/mediabrowser/system/shutdown", "");
  113. try
  114. {
  115. server.WaitForExit();
  116. }
  117. catch (ArgumentException)
  118. {
  119. // already gone
  120. }
  121. }
  122. catch (WebException ex)
  123. {
  124. if (ex.Status != WebExceptionStatus.ConnectFailure && !ex.Message.StartsWith("Unable to connect", StringComparison.OrdinalIgnoreCase))
  125. {
  126. MessageBox.Show("Error shutting down server. Please be sure it is not running before hitting OK.\n\n" + ex.Status + "\n\n" + ex.Message);
  127. }
  128. }
  129. }
  130. }
  131. }
  132. else
  133. {
  134. // Installing MBT - shut it down if it is running
  135. var processes = Process.GetProcessesByName("mediabrowser.ui");
  136. if (processes.Length > 0)
  137. {
  138. lblHeading.Content = "Shutting Down Media Browser Theater...";
  139. try
  140. {
  141. processes[0].Kill();
  142. }
  143. catch (Exception ex)
  144. {
  145. MessageBox.Show("Unable to shutdown Media Browser Theater. Please ensure it is not running before hitting OK.\n\n" + ex.Message, "Error");
  146. }
  147. }
  148. }
  149. // if the startmenu item is empty now - delete it too
  150. if (Directory.GetFiles(startMenu).Length == 0)
  151. {
  152. try
  153. {
  154. Directory.Delete(startMenu);
  155. }
  156. catch (DirectoryNotFoundException)
  157. {
  158. }
  159. catch (Exception ex)
  160. {
  161. {
  162. MessageBox.Show(string.Format("Error attempting to remove shortcut folder {0}\n\n {1}", startMenu, ex.Message), "Error");
  163. }
  164. }
  165. }
  166. var rootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MediaBrowser" + RootSuffix);
  167. lblHeading.Content = "Removing System Files...";
  168. if (cbxRemoveAll.IsChecked == true)
  169. {
  170. // Just remove our whole directory
  171. RemovePath(rootPath);
  172. }
  173. else
  174. {
  175. // First remove the system
  176. RemovePath(Path.Combine(rootPath, "System"));
  177. RemovePath(Path.Combine(rootPath, "MediaTools"));
  178. // And then the others specified
  179. if (cbxRemoveCache.IsChecked == true)
  180. {
  181. lblHeading.Content = "Removing Cache and Data Files...";
  182. RemovePath(Path.Combine(rootPath, "cache"));
  183. RemovePath(Path.Combine(rootPath, "data"));
  184. }
  185. if (cbxRemoveConfig.IsChecked == true)
  186. {
  187. lblHeading.Content = "Removing Config Files...";
  188. RemovePath(Path.Combine(rootPath, "config"));
  189. RemovePath(Path.Combine(rootPath, "logs"));
  190. }
  191. if (cbxRemovePlugins.IsChecked == true)
  192. {
  193. lblHeading.Content = "Removing Plugin Files...";
  194. RemovePath(Path.Combine(rootPath, "plugins"));
  195. }
  196. }
  197. // Remove reference to us
  198. RemoveUninstall();
  199. // Remove pismo
  200. try
  201. {
  202. UnInstallPismo();
  203. }
  204. catch
  205. {
  206. // No biggie - maybe they uninstalled it themselves
  207. }
  208. // and done
  209. lblHeading.Content = string.Format("Media Browser {0} Uninstalled.", Product);
  210. btnUninstall.Visibility = Visibility.Hidden;
  211. btnFinished.Visibility = Visibility.Visible;
  212. }
  213. private void UnInstallPismo()
  214. {
  215. // Kick off the Pismo uninstaller and wait for it to end
  216. var pismo = new Process();
  217. pismo.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  218. pismo.StartInfo.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "pfm.exe");
  219. pismo.StartInfo.Arguments = "uninstall pfm-license-mediabrowser.txt";
  220. pismo.Start();
  221. pismo.WaitForExit();
  222. }
  223. private void RemoveUninstall()
  224. {
  225. using (var parent = Registry.CurrentUser.OpenSubKey(
  226. @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", true))
  227. {
  228. if (parent == null)
  229. {
  230. MessageBox.Show("Uninstall registry key not found.");
  231. return;
  232. }
  233. try
  234. {
  235. const string guidText = "{4E76DB4E-1BB9-4A7B-860C-7940779CF7A0}";
  236. parent.DeleteSubKey(guidText,false);
  237. }
  238. catch (Exception ex)
  239. {
  240. throw new Exception(
  241. "An error occurred removing uninstall information from the registry.",
  242. ex);
  243. }
  244. }
  245. }
  246. private static
  247. void RemoveShortcut(string path)
  248. {
  249. try
  250. {
  251. File.Delete(path);
  252. }
  253. catch (FileNotFoundException)
  254. {
  255. } // we're trying to get rid of it anyway
  256. catch (Exception ex)
  257. {
  258. MessageBox.Show(string.Format("Error attempting to remove shortcut {0}\n\n {1}", path, ex.Message), "Error");
  259. }
  260. }
  261. private static void RemovePath(string path)
  262. {
  263. try
  264. {
  265. Directory.Delete(path, true);
  266. }
  267. catch (DirectoryNotFoundException)
  268. {
  269. }
  270. catch (Exception ex)
  271. {
  272. MessageBox.Show(string.Format("Error attempting to remove progam folder {0}\n\n {1}", path, ex.Message), "Error");
  273. }
  274. }
  275. private void BtnFinished_OnClick(object sender, RoutedEventArgs e)
  276. {
  277. Close();
  278. }
  279. }
  280. }