MainWindow.xaml.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. using (var client = new WebClient())
  68. {
  69. lblHeading.Content = "Shutting Down Server...";
  70. try
  71. {
  72. client.UploadString("http://localhost:8096/mediabrowser/system/shutdown", "");
  73. }
  74. catch (WebException ex)
  75. {
  76. if (ex.Status != WebExceptionStatus.ConnectFailure && !ex.Message.StartsWith("Unable to connect", StringComparison.OrdinalIgnoreCase))
  77. {
  78. MessageBox.Show("Error shutting down server. Please be sure it is not running before hitting OK.\n\n" + ex.Status + "\n\n" + ex.Message);
  79. }
  80. }
  81. }
  82. }
  83. // if the startmenu item is empty now - delete it too
  84. if (Directory.GetFiles(startMenu).Length == 0)
  85. {
  86. try
  87. {
  88. Directory.Delete(startMenu);
  89. }
  90. catch (DirectoryNotFoundException)
  91. {
  92. }
  93. catch (Exception ex)
  94. {
  95. {
  96. MessageBox.Show(string.Format("Error attempting to remove shortcut folder {0}\n\n {1}", startMenu, ex.Message), "Error");
  97. }
  98. }
  99. }
  100. var rootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MediaBrowser" + RootSuffix);
  101. lblHeading.Content = "Removing System Files...";
  102. if (cbxRemoveAll.IsChecked == true)
  103. {
  104. // Just remove our whole directory
  105. RemovePath(rootPath);
  106. }
  107. else
  108. {
  109. // First remove the system
  110. RemovePath(Path.Combine(rootPath, "System"));
  111. RemovePath(Path.Combine(rootPath, "MediaTools"));
  112. // And then the others specified
  113. if (cbxRemoveCache.IsChecked == true)
  114. {
  115. lblHeading.Content = "Removing Cache and Data Files...";
  116. RemovePath(Path.Combine(rootPath, "cache"));
  117. RemovePath(Path.Combine(rootPath, "data"));
  118. }
  119. if (cbxRemoveConfig.IsChecked == true)
  120. {
  121. lblHeading.Content = "Removing Config Files...";
  122. RemovePath(Path.Combine(rootPath, "config"));
  123. RemovePath(Path.Combine(rootPath, "logs"));
  124. }
  125. if (cbxRemovePlugins.IsChecked == true)
  126. {
  127. lblHeading.Content = "Removing Plugin Files...";
  128. RemovePath(Path.Combine(rootPath, "plugins"));
  129. }
  130. }
  131. // and done
  132. lblHeading.Content = string.Format("Media Browser {0} Uninstalled.", Product);
  133. btnUninstall.Visibility = Visibility.Hidden;
  134. btnFinished.Visibility = Visibility.Visible;
  135. }
  136. private static void RemoveShortcut(string path)
  137. {
  138. try
  139. {
  140. File.Delete(path);
  141. }
  142. catch (FileNotFoundException)
  143. {
  144. } // we're trying to get rid of it anyway
  145. catch (Exception ex)
  146. {
  147. MessageBox.Show(string.Format("Error attempting to remove shortcut {0}\n\n {1}", path, ex.Message), "Error");
  148. }
  149. }
  150. private static void RemovePath(string path)
  151. {
  152. try
  153. {
  154. Directory.Delete(path, true);
  155. }
  156. catch (DirectoryNotFoundException)
  157. {
  158. }
  159. catch (Exception ex)
  160. {
  161. MessageBox.Show(string.Format("Error attempting to remove progam folder {0}\n\n {1}", path, ex.Message), "Error");
  162. }
  163. }
  164. private void BtnFinished_OnClick(object sender, RoutedEventArgs e)
  165. {
  166. Close();
  167. }
  168. }
  169. }