UWPHelper.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System.Collections.Generic;
  2. using System.Management.Automation;
  3. namespace Optimizer
  4. {
  5. internal static class UWPHelper
  6. {
  7. internal static List<KeyValuePair<string, string>> GetUWPApps(bool showAll)
  8. {
  9. List<KeyValuePair<string, string>> modernApps = new List<KeyValuePair<string, string>>();
  10. using (PowerShell script = PowerShell.Create())
  11. {
  12. if (showAll)
  13. {
  14. script.AddScript("Get-AppxPackage | Select Name,InstallLocation");
  15. }
  16. else
  17. {
  18. script.AddScript(@"Get-AppxPackage | Where {$_.NonRemovable -like ""False""} | Select Name,InstallLocation");
  19. }
  20. string[] tmp;
  21. foreach (PSObject x in script.Invoke())
  22. {
  23. tmp = x.ToString().Replace("@", string.Empty).Replace("{", string.Empty).Replace("}", string.Empty).Replace("Name=", string.Empty).Replace("InstallLocation=", string.Empty).Trim().Split(';');
  24. modernApps.Add(new KeyValuePair<string, string>(tmp[0], tmp[1]));
  25. }
  26. }
  27. return modernApps;
  28. }
  29. internal static bool UninstallUWPApp(string appName)
  30. {
  31. using (PowerShell script = PowerShell.Create())
  32. {
  33. script.AddScript(string.Format("Get-AppxPackage -AllUsers '{0}' | Remove-AppxPackage", appName));
  34. script.Invoke();
  35. return script.Streams.Error.Count > 0;
  36. // not working on Windows 7 anymore
  37. //return script.HadErrors;
  38. }
  39. }
  40. // TODO: Reinstall default pre-installed apps
  41. }
  42. }