MainWindow.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Diagnostics;
  3. using System.Reflection;
  4. using System.IO;
  5. using System.Threading;
  6. using System.Windows;
  7. namespace MediaBrowser.Uninstaller.Execute
  8. {
  9. /// <summary>
  10. /// Interaction logic for MainWindow.xaml
  11. /// </summary>
  12. public partial class MainWindow : Window
  13. {
  14. protected string Product = "Server";
  15. protected string RootSuffix = "-Server";
  16. public MainWindow()
  17. {
  18. Thread.Sleep(800); // be sure our caller is shut down
  19. var args = Environment.GetCommandLineArgs();
  20. var product = args.Length > 1 ? args[1] : "server";
  21. InitializeComponent();
  22. switch (product)
  23. {
  24. case "server":
  25. Product = "Server";
  26. RootSuffix = "-Server";
  27. break;
  28. case "mbt":
  29. Product = "Theater";
  30. RootSuffix = "-UI";
  31. break;
  32. default:
  33. MessageBox.Show("Please specify which application to un-install (server or mbt)");
  34. Close();
  35. break;
  36. }
  37. lblHeading.Content = this.Title = "Uninstall Media Browser " + Product;
  38. }
  39. private void btnCancel_Click(object sender, RoutedEventArgs e)
  40. {
  41. Close();
  42. }
  43. private void cbxRemoveAll_Checked(object sender, RoutedEventArgs e)
  44. {
  45. if (cbxRemoveAll.IsChecked == true)
  46. {
  47. cbxRemoveCache.IsChecked = cbxRemoveConfig.IsChecked = cbxRemovePlugins.IsChecked = true;
  48. }
  49. cbxRemoveCache.IsEnabled = cbxRemoveConfig.IsEnabled = cbxRemovePlugins.IsEnabled = !cbxRemoveAll.IsChecked.Value;
  50. }
  51. private void btnUninstall_Click(object sender, RoutedEventArgs e)
  52. {
  53. // First remove our shortcuts
  54. lblHeading.Content = "Removing Shortcuts...";
  55. btnCancel.IsEnabled = btnUninstall.IsEnabled = false;
  56. grdOptions.Visibility = Visibility.Hidden;
  57. var startMenu = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "Media Browser 3");
  58. var linkName = "Media Browser " + Product + ".lnk";
  59. RemoveShortcut(Path.Combine(startMenu, linkName));
  60. RemoveShortcut(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup),linkName));
  61. linkName = "Uninstall " + linkName;
  62. RemoveShortcut(Path.Combine(startMenu, linkName));
  63. if (Product == "Server")
  64. {
  65. RemoveShortcut(Path.Combine(startMenu, "MB Dashboard.lnk"));
  66. }
  67. // if the startmenu item is empty now - delete it too
  68. if (Directory.GetFiles(startMenu).Length == 0)
  69. {
  70. try
  71. {
  72. Directory.Delete(startMenu);
  73. }
  74. catch (DirectoryNotFoundException)
  75. {
  76. }
  77. catch (Exception ex)
  78. {
  79. {
  80. MessageBox.Show(string.Format("Error attempting to remove shortcut folder {0}\n\n {1}", startMenu, ex.Message), "Error");
  81. }
  82. }
  83. }
  84. var rootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MediaBrowser" + RootSuffix);
  85. if (cbxRemoveAll.IsChecked == true)
  86. {
  87. // Just remove our whole directory
  88. RemovePath(rootPath);
  89. }
  90. else
  91. {
  92. // First remove the system
  93. lblHeading.Content = "Removing System Files...";
  94. RemovePath(Path.Combine(rootPath, "System"));
  95. RemovePath(Path.Combine(rootPath, "MediaTools"));
  96. // And then the others specified
  97. if (cbxRemoveCache.IsChecked == true)
  98. {
  99. lblHeading.Content = "Removing Cache and Data Files...";
  100. RemovePath(Path.Combine(rootPath, "cache"));
  101. RemovePath(Path.Combine(rootPath, "data"));
  102. }
  103. if (cbxRemoveConfig.IsChecked == true)
  104. {
  105. lblHeading.Content = "Removing Config Files...";
  106. RemovePath(Path.Combine(rootPath, "config"));
  107. RemovePath(Path.Combine(rootPath, "logs"));
  108. }
  109. if (cbxRemovePlugins.IsChecked == true)
  110. {
  111. lblHeading.Content = "Removing Plugin Files...";
  112. RemovePath(Path.Combine(rootPath, "plugins"));
  113. }
  114. }
  115. // and done
  116. lblHeading.Content = string.Format("Media Browser {0} Uninstalled.", Product);
  117. btnUninstall.Visibility = Visibility.Hidden;
  118. btnFinished.Visibility = Visibility.Visible;
  119. }
  120. private static void RemoveShortcut(string path)
  121. {
  122. try
  123. {
  124. File.Delete(path);
  125. }
  126. catch (FileNotFoundException)
  127. {
  128. } // we're trying to get rid of it anyway
  129. catch (Exception ex)
  130. {
  131. MessageBox.Show(string.Format("Error attempting to remove shortcut {0}\n\n {1}", path, ex.Message), "Error");
  132. }
  133. }
  134. private static void RemovePath(string path)
  135. {
  136. try
  137. {
  138. Directory.Delete(path, true);
  139. }
  140. catch (DirectoryNotFoundException)
  141. {
  142. }
  143. catch (Exception ex)
  144. {
  145. MessageBox.Show(string.Format("Error attempting to remove progam folder {0}\n\n {1}", path, ex.Message), "Error");
  146. }
  147. }
  148. private void BtnFinished_OnClick(object sender, RoutedEventArgs e)
  149. {
  150. Close();
  151. }
  152. }
  153. }