2
0

MainWindow.xaml.cs 9.4 KB

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