MainWindow.xaml.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Diagnostics;
  3. using System.Net;
  4. using System.Reflection;
  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. Thread.Sleep(800); // be sure our caller is shut down
  20. var args = Environment.GetCommandLineArgs();
  21. var product = args.Length > 1 ? args[1] : "server";
  22. InitializeComponent();
  23. switch (product)
  24. {
  25. case "server":
  26. Product = "Server";
  27. RootSuffix = "-Server";
  28. break;
  29. case "mbt":
  30. Product = "Theater";
  31. RootSuffix = "-UI";
  32. break;
  33. default:
  34. MessageBox.Show("Please specify which application to un-install (server or mbt)");
  35. Close();
  36. break;
  37. }
  38. lblHeading.Content = this.Title = "Uninstall Media Browser " + Product;
  39. }
  40. private void btnCancel_Click(object sender, RoutedEventArgs e)
  41. {
  42. Close();
  43. }
  44. private void cbxRemoveAll_Checked(object sender, RoutedEventArgs e)
  45. {
  46. if (cbxRemoveAll.IsChecked == true)
  47. {
  48. cbxRemoveCache.IsChecked = cbxRemoveConfig.IsChecked = cbxRemovePlugins.IsChecked = true;
  49. }
  50. cbxRemoveCache.IsEnabled = cbxRemoveConfig.IsEnabled = cbxRemovePlugins.IsEnabled = !cbxRemoveAll.IsChecked.Value;
  51. }
  52. private void btnUninstall_Click(object sender, RoutedEventArgs e)
  53. {
  54. // First remove our shortcuts
  55. lblHeading.Content = "Removing Shortcuts...";
  56. btnCancel.IsEnabled = btnUninstall.IsEnabled = false;
  57. grdOptions.Visibility = Visibility.Hidden;
  58. var startMenu = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "Media Browser 3");
  59. var linkName = "Media Browser " + Product + ".lnk";
  60. RemoveShortcut(Path.Combine(startMenu, linkName));
  61. RemoveShortcut(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup),linkName));
  62. linkName = "Uninstall " + linkName;
  63. RemoveShortcut(Path.Combine(startMenu, linkName));
  64. if (Product == "Server")
  65. {
  66. RemoveShortcut(Path.Combine(startMenu, "MB Dashboard.lnk"));
  67. if (Process.GetProcessesByName("mediabrowser.serverapplication").Length != 0)
  68. {
  69. using (var client = new WebClient())
  70. {
  71. lblHeading.Content = "Shutting Down Media Browser Server...";
  72. try
  73. {
  74. client.UploadString("http://localhost:8096/mediabrowser/system/shutdown", "");
  75. }
  76. catch (WebException ex)
  77. {
  78. if (ex.Status != WebExceptionStatus.ConnectFailure && !ex.Message.StartsWith("Unable to connect", StringComparison.OrdinalIgnoreCase))
  79. {
  80. MessageBox.Show("Error shutting down server. Please be sure it is not running before hitting OK.\n\n" + ex.Status + "\n\n" + ex.Message);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. else
  87. {
  88. // Installing MBT - shut it down if it is running
  89. var processes = Process.GetProcessesByName("mediabrowser.ui");
  90. if (processes.Length > 0)
  91. {
  92. lblHeading.Content = "Shutting Down Media Browser Theater...";
  93. try
  94. {
  95. processes[0].Kill();
  96. }
  97. catch (Exception ex)
  98. {
  99. MessageBox.Show("Unable to shutdown Media Browser Theater. Please ensure it is not running before hitting OK.\n\n" + ex.Message, "Error");
  100. }
  101. }
  102. }
  103. // if the startmenu item is empty now - delete it too
  104. if (Directory.GetFiles(startMenu).Length == 0)
  105. {
  106. try
  107. {
  108. Directory.Delete(startMenu);
  109. }
  110. catch (DirectoryNotFoundException)
  111. {
  112. }
  113. catch (Exception ex)
  114. {
  115. {
  116. MessageBox.Show(string.Format("Error attempting to remove shortcut folder {0}\n\n {1}", startMenu, ex.Message), "Error");
  117. }
  118. }
  119. }
  120. var rootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MediaBrowser" + RootSuffix);
  121. lblHeading.Content = "Removing System Files...";
  122. if (cbxRemoveAll.IsChecked == true)
  123. {
  124. // Just remove our whole directory
  125. RemovePath(rootPath);
  126. }
  127. else
  128. {
  129. // First remove the system
  130. RemovePath(Path.Combine(rootPath, "System"));
  131. RemovePath(Path.Combine(rootPath, "MediaTools"));
  132. // And then the others specified
  133. if (cbxRemoveCache.IsChecked == true)
  134. {
  135. lblHeading.Content = "Removing Cache and Data Files...";
  136. RemovePath(Path.Combine(rootPath, "cache"));
  137. RemovePath(Path.Combine(rootPath, "data"));
  138. }
  139. if (cbxRemoveConfig.IsChecked == true)
  140. {
  141. lblHeading.Content = "Removing Config Files...";
  142. RemovePath(Path.Combine(rootPath, "config"));
  143. RemovePath(Path.Combine(rootPath, "logs"));
  144. }
  145. if (cbxRemovePlugins.IsChecked == true)
  146. {
  147. lblHeading.Content = "Removing Plugin Files...";
  148. RemovePath(Path.Combine(rootPath, "plugins"));
  149. }
  150. }
  151. // and done
  152. lblHeading.Content = string.Format("Media Browser {0} Uninstalled.", Product);
  153. btnUninstall.Visibility = Visibility.Hidden;
  154. btnFinished.Visibility = Visibility.Visible;
  155. }
  156. private static void RemoveShortcut(string path)
  157. {
  158. try
  159. {
  160. File.Delete(path);
  161. }
  162. catch (FileNotFoundException)
  163. {
  164. } // we're trying to get rid of it anyway
  165. catch (Exception ex)
  166. {
  167. MessageBox.Show(string.Format("Error attempting to remove shortcut {0}\n\n {1}", path, ex.Message), "Error");
  168. }
  169. }
  170. private static void RemovePath(string path)
  171. {
  172. try
  173. {
  174. Directory.Delete(path, true);
  175. }
  176. catch (DirectoryNotFoundException)
  177. {
  178. }
  179. catch (Exception ex)
  180. {
  181. MessageBox.Show(string.Format("Error attempting to remove progam folder {0}\n\n {1}", path, ex.Message), "Error");
  182. }
  183. }
  184. private void BtnFinished_OnClick(object sender, RoutedEventArgs e)
  185. {
  186. Close();
  187. }
  188. }
  189. }