UWPHelper.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. internal static bool RestoreAllUWPApps()
  41. {
  42. string cmd = "Get-AppxPackage -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register \"$($_.InstallLocation)\\AppXManifest.xml\"}";
  43. using (PowerShell script = PowerShell.Create())
  44. {
  45. script.AddScript(cmd);
  46. script.Invoke();
  47. return script.Streams.Error.Count > 0;
  48. }
  49. }
  50. }
  51. }