Skip to content

Commit

Permalink
Added the possibility to get UWP apps asynchronously (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpeyr committed Jun 30, 2023
1 parent 74e5025 commit 6ff45ae
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions PeyrSharp.Env/Sys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text.Json;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace PeyrSharp.Env
{
Expand Down Expand Up @@ -248,5 +251,52 @@ public static bool TerminateProcess(int processId)
return false;
}
}

/// <summary>
/// Retrieves a list of UWP (Universal Windows Platform) apps asynchronously.
/// </summary>
/// <remarks>Only works on Windows 10 and higher.</remarks>
/// <returns>A task that represents the asynchronous operation. The task result contains a list of UwpApp objects representing the UWP apps.</returns>
/// <exception cref="FileNotFoundException"></exception>
/// <exception cref="Win32Exception"></exception>
[SupportedOSPlatform("windows")]
public static async Task<List<UwpApp>> GetUwpAppsAsync()
{
ProcessStartInfo pInfo = new()
{
FileName = "powershell.exe",
Arguments = @"& Get-StartApps | ConvertTo-Json > $env:appdata\UwpApps.json",
UseShellExecute = true,
CreateNoWindow = true
};

Process process = new() { StartInfo = pInfo };
process.Start();
await process.WaitForExitAsync();

string appsFile = await File.ReadAllTextAsync($@"{FileSys.AppDataPath}\UWPapps.json");
List<UwpApp> apps = JsonSerializer.Deserialize<List<UwpApp>>(appsFile); // Get apps
List<UwpApp> sortedApps = new(); // Create final list
Dictionary<UwpApp, string> uwpApps = new(); // Create a dictionnary

// Sort apps to only have UWP apps (they have a "!" in the AppID property)
for (int i = 0; i < apps.Count; i++)
{
if (apps[i].AppID.Contains('!'))
{
uwpApps.Add(apps[i], apps[i].Name);
}
}

// Sort alphabetically
var sorted = from pair in uwpApps orderby pair.Value ascending select pair; // Sort

foreach (KeyValuePair<UwpApp, string> pair1 in sorted)
{
sortedApps.Add(pair1.Key);
}

return sortedApps; // Return
}
}
}

0 comments on commit 6ff45ae

Please sign in to comment.