MainWindow.xaml.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 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. // if the startmenu item is empty now - delete it too
  87. if (Directory.GetFiles(startMenu).Length == 0)
  88. {
  89. try
  90. {
  91. Directory.Delete(startMenu);
  92. }
  93. catch (DirectoryNotFoundException)
  94. {
  95. }
  96. catch (Exception ex)
  97. {
  98. {
  99. MessageBox.Show(string.Format("Error attempting to remove shortcut folder {0}\n\n {1}", startMenu, ex.Message), "Error");
  100. }
  101. }
  102. }
  103. var rootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MediaBrowser" + RootSuffix);
  104. lblHeading.Content = "Removing System Files...";
  105. if (cbxRemoveAll.IsChecked == true)
  106. {
  107. // Just remove our whole directory
  108. RemovePath(rootPath);
  109. }
  110. else
  111. {
  112. // First remove the system
  113. RemovePath(Path.Combine(rootPath, "System"));
  114. RemovePath(Path.Combine(rootPath, "MediaTools"));
  115. // And then the others specified
  116. if (cbxRemoveCache.IsChecked == true)
  117. {
  118. lblHeading.Content = "Removing Cache and Data Files...";
  119. RemovePath(Path.Combine(rootPath, "cache"));
  120. RemovePath(Path.Combine(rootPath, "data"));
  121. }
  122. if (cbxRemoveConfig.IsChecked == true)
  123. {
  124. lblHeading.Content = "Removing Config Files...";
  125. RemovePath(Path.Combine(rootPath, "config"));
  126. RemovePath(Path.Combine(rootPath, "logs"));
  127. }
  128. if (cbxRemovePlugins.IsChecked == true)
  129. {
  130. lblHeading.Content = "Removing Plugin Files...";
  131. RemovePath(Path.Combine(rootPath, "plugins"));
  132. }
  133. }
  134. // and done
  135. lblHeading.Content = string.Format("Media Browser {0} Uninstalled.", Product);
  136. btnUninstall.Visibility = Visibility.Hidden;
  137. btnFinished.Visibility = Visibility.Visible;
  138. }
  139. private static void RemoveShortcut(string path)
  140. {
  141. try
  142. {
  143. File.Delete(path);
  144. }
  145. catch (FileNotFoundException)
  146. {
  147. } // we're trying to get rid of it anyway
  148. catch (Exception ex)
  149. {
  150. MessageBox.Show(string.Format("Error attempting to remove shortcut {0}\n\n {1}", path, ex.Message), "Error");
  151. }
  152. }
  153. private static void RemovePath(string path)
  154. {
  155. try
  156. {
  157. Directory.Delete(path, true);
  158. }
  159. catch (DirectoryNotFoundException)
  160. {
  161. }
  162. catch (Exception ex)
  163. {
  164. MessageBox.Show(string.Format("Error attempting to remove progam folder {0}\n\n {1}", path, ex.Message), "Error");
  165. }
  166. }
  167. private void BtnFinished_OnClick(object sender, RoutedEventArgs e)
  168. {
  169. Close();
  170. }
  171. }
  172. }