/source/UninstallTools/Factory/StoreAppFactory.cs

https://github.com/Klocman/Bulk-Crap-Uninstaller · C# · 108 lines · 85 code · 15 blank · 8 comment · 12 complexity · fcd80a41b7ffc047aac62e80d4e03579 MD5 · raw file

  1. /*
  2. Copyright (c) 2017 Marcin Szeniak (https://github.com/Klocman/)
  3. Apache License Version 2.0
  4. */
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.Linq;
  11. using Klocman.Native;
  12. using Klocman.Tools;
  13. using UninstallTools.Properties;
  14. namespace UninstallTools.Factory
  15. {
  16. public class StoreAppFactory : IIndependantUninstallerFactory
  17. {
  18. static StoreAppFactory()
  19. {
  20. var storeAppHelperPath = Path.Combine(UninstallToolsGlobalConfig.AssemblyLocation, @"StoreAppHelper.exe");
  21. if (WindowsTools.CheckNetFramework4Installed(true) != null && File.Exists(storeAppHelperPath))
  22. StoreAppHelperPath = storeAppHelperPath;
  23. }
  24. public static string GetPowerShellRemoveCommand(string fullName)
  25. {
  26. return $"Remove-AppxPackage -package {fullName} -confirm:$false";
  27. }
  28. /// <summary>
  29. /// Convert supplied store app entries to power shell uninstall commands. Non-store app entries are ignored.
  30. /// </summary>
  31. public static string[] ToPowerShellRemoveCommands(IEnumerable<ApplicationUninstallerEntry> appEntries)
  32. {
  33. return appEntries.Where(x => x.UninstallerKind == UninstallerType.StoreApp)
  34. .Select(x => GetPowerShellRemoveCommand(x.Comment))
  35. .ToArray();
  36. }
  37. private static string StoreAppHelperPath { get; }
  38. public IList<ApplicationUninstallerEntry> GetUninstallerEntries(
  39. ListGenerationProgress.ListGenerationCallback progressCallback)
  40. {
  41. var results = new List<ApplicationUninstallerEntry>();
  42. if (StoreAppHelperPath == null) return results;
  43. var output = FactoryTools.StartHelperAndReadOutput(StoreAppHelperPath, "/query");
  44. if (string.IsNullOrEmpty(output)) return results;
  45. var windowsPath = WindowsTools.GetEnvironmentPath(CSIDL.CSIDL_WINDOWS);
  46. foreach (var data in FactoryTools.ExtractAppDataSetsFromHelperOutput(output))
  47. {
  48. if (!data.ContainsKey("InstalledLocation") || !Directory.Exists(data["InstalledLocation"])) continue;
  49. var fullName = data["FullName"];
  50. var uninstallStr = $"\"{StoreAppHelperPath}\" /uninstall \"{fullName}\"";
  51. var isProtected = data.ContainsKey("IsProtected") && Convert.ToBoolean(data["IsProtected"], CultureInfo.InvariantCulture);
  52. var result = new ApplicationUninstallerEntry
  53. {
  54. Comment = fullName,
  55. CacheIdOverride = fullName,
  56. RatingId = fullName.Substring(0, fullName.IndexOf("_", StringComparison.Ordinal)),
  57. UninstallString = uninstallStr,
  58. QuietUninstallString = uninstallStr,
  59. RawDisplayName = string.IsNullOrEmpty(data["DisplayName"]) ? fullName : data["DisplayName"],
  60. Publisher = data["PublisherDisplayName"],
  61. IsValid = true,
  62. UninstallerKind = UninstallerType.StoreApp,
  63. InstallLocation = data["InstalledLocation"],
  64. InstallDate = Directory.GetCreationTime(data["InstalledLocation"]),
  65. IsProtected = isProtected,
  66. SystemComponent = isProtected
  67. };
  68. if (File.Exists(data["Logo"]))
  69. {
  70. try
  71. {
  72. result.DisplayIcon = data["Logo"];
  73. result.IconBitmap = DrawingTools.IconFromImage(new Bitmap(data["Logo"]));
  74. }
  75. catch
  76. {
  77. result.DisplayIcon = null;
  78. result.IconBitmap = null;
  79. }
  80. }
  81. if (result.InstallLocation.StartsWith(windowsPath, StringComparison.InvariantCultureIgnoreCase))
  82. {
  83. result.SystemComponent = true;
  84. //result.IsProtected = true;
  85. }
  86. results.Add(result);
  87. }
  88. return results;
  89. }
  90. public bool IsEnabled() => UninstallToolsGlobalConfig.ScanStoreApps;
  91. public string DisplayName => Localisation.Progress_AppStores_WinStore;
  92. }
  93. }