MainWindow.xaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. public MainWindow()
  16. {
  17. Thread.Sleep(800); // be sure our caller is shut down
  18. var args = Environment.GetCommandLineArgs();
  19. var product = args.Length > 1 ? args[1] : "server";
  20. InitializeComponent();
  21. switch (product)
  22. {
  23. case "server":
  24. Product = "Server";
  25. break;
  26. case "mbt":
  27. Product = "Theater";
  28. break;
  29. default:
  30. MessageBox.Show("Please specify which application to un-install (server or mbt)");
  31. Close();
  32. break;
  33. }
  34. lblHeading.Content = this.Title = "Uninstall Media Browser " + Product;
  35. }
  36. private void btnCancel_Click(object sender, RoutedEventArgs e)
  37. {
  38. Close();
  39. }
  40. private void cbxRemoveAll_Checked(object sender, RoutedEventArgs e)
  41. {
  42. if (cbxRemoveAll.IsChecked == true)
  43. {
  44. cbxRemoveCache.IsChecked = cbxRemoveConfig.IsChecked = cbxRemovePlugins.IsChecked = true;
  45. }
  46. cbxRemoveCache.IsEnabled = cbxRemoveConfig.IsEnabled = cbxRemovePlugins.IsEnabled = !cbxRemoveAll.IsChecked.Value;
  47. }
  48. private void btnUninstall_Click(object sender, RoutedEventArgs e)
  49. {
  50. // First remove our shortcuts
  51. lblHeading.Content = "Removing Shortcuts...";
  52. btnCancel.IsEnabled = btnUninstall.IsEnabled = false;
  53. grdOptions.Visibility = Visibility.Hidden;
  54. var startMenu = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "Media Browser 3");
  55. var linkName = "Media Browser " + Product + ".lnk";
  56. RemoveShortcut(Path.Combine(startMenu, linkName));
  57. linkName = "Uninstall " + linkName;
  58. RemoveShortcut(Path.Combine(startMenu, linkName));
  59. if (Product == "Server")
  60. {
  61. RemoveShortcut(Path.Combine(startMenu, "Dashboard.lnk"));
  62. }
  63. // if the startmenu item is empty now - delete it too
  64. if (Directory.GetFiles(startMenu).Length == 0)
  65. {
  66. try
  67. {
  68. Directory.Delete(startMenu);
  69. }
  70. catch (DirectoryNotFoundException)
  71. {
  72. }
  73. catch (Exception ex)
  74. {
  75. {
  76. MessageBox.Show(string.Format("Error attempting to remove shortcut folder {0}\n\n {1}", startMenu, ex.Message), "Error");
  77. }
  78. }
  79. }
  80. // and done
  81. lblHeading.Content = string.Format("Media Browser {0} Uninstalled.", Product);
  82. btnUninstall.Content = "Finish";
  83. }
  84. private static void RemoveShortcut(string path)
  85. {
  86. try
  87. {
  88. File.Delete(path);
  89. }
  90. catch (FileNotFoundException)
  91. {
  92. } // we're trying to get rid of it anyway
  93. catch (Exception ex)
  94. {
  95. MessageBox.Show(string.Format("Error attempting to remove shortcut {0}\n\n {1}", path, ex.Message), "Error");
  96. }
  97. }
  98. }
  99. }