简体   繁体   中英

Programmatically get list of installed application executables (Windows10, C#)

I want to get a list of applications that are installed and executable on Windows 10 -- ie executables of applications a user can launch (UWP and non-UWP). For UWP apps, I want to get its AppUserModelId. For non-UWP apps, I want to get the executable's file location.

So far, I have tried: 1) Going through the registry and iterate through the installed applications . However, this method goes through all installed applications, the returned list would include things like microsoft asp.net core 2.1.8 shared framework (x64) , which I want to eliminate.

2) I found that I can access what I wanted through the special Appsfolder , by typing shell:Appsfolder into either the explorer window or Run. So far I was able to get access to this folder, and iterate through the items and list the names of the files. But I am not sure how to collect information on AppUserModelId (for UWP apps) and filepath (for WPF apps).

Relevant code I used to enumerate Appsfolder files:

namespace AppsFolder
{
    ...
    public static class AppsFolder
    {
       ...

       // GUID for shell:Appsfolder
       public static readonly Guid KnownFolderID = new Guid("1e87508d-89c2-42f0-8a7e-645a0f50ca58");

       public static IEnumerable<string> GetItemNames(ESHGDN gdn)
       {
           IShellFolder appsFolder = GetAppsFolder();
           try
           {
               IntPtr enumIDListInterfacePointer;
               appsFolder.EnumObjects(IntPtr.Zero,
                    ESHCONTF.SHCONTF_NONFOLDERS | ESHCONTF.SHCONTF_FOLDERS,
                   out enumIDListInterfacePointer);
               using (var enumIDList = new
                      EnumIDListWrapper(enumIDListInterfacePointer))
               {
                  var names = new List<string>();
                  foreach (var pidl in enumIDList)
                  {
                      STRRET name;
                      recycleBin.GetDisplayNameOf(
                              pidl.DangerousGetHandle(),
                             gdn,
                             out name);
                      names.Add(STRRETToString(ref name, pidl));
                      if (name.uType != (int)STRRETType.Offset)
                         Marshal.FreeCoTaskMem(name.data.pOleStr);
                  }
                  return names;
               }
           }
           finally
           {
               Marshal.FinalReleaseComObject(recycleBin);
           }
        }

        private static IShellFolder GetAppsFolder()
        {
            IShellFolder desktop;
            NativeMethod.SHGetDesktopFolder(out desktop);
            try
            {
                Guid shellFolderInterfaceId = typeof(IShellFolder).GUID;
                IntPtr recycleBinShellFolderInterfacePointer;
                desktop.BindToObject(
                    GetItemIDList().DangerousGetHandle(),
                    IntPtr.Zero,
                    ref shellFolderInterfaceId,
                    out recycleBinShellFolderInterfacePointer);
                return (IShellFolder)Marshal.GetObjectForIUnknown(
                             recycleBinShellFolderInterfacePointer);
            }
            finally
            {
                Marshal.FinalReleaseComObject(desktop);
            }
         }

         public static SafeCoTaskMemHandleZeroIsInvalid GetItemIDList()
         {
             SafeCoTaskMemHandleZeroIsInvalid pidl;
             Guid guid = KnownFolderID;
             NativeMethod.SHGetKnownFolderIDList(
                ref guid, 0, IntPtr.Zero, out pidl);
             return pidl;
         }
     }
}

Executed as:

string[] names = AppsFolder.GetItemNames(AppsFolder.ESHGDN.SHGDN_INFOLDER).ToArray();
for (int i=0; i<names.Length; i++)
{
    Console.WriteLine(names[i]);
}

The ILaunchSourceAppUserModelID interface provides a method to get the AppUserModelID, but I'm not sure how to get access to this interface through my IShellFolder interface. Also, how do I get the actual file location within the AppsFolder virtual folder to actual exe's filepath?

You can get Application User Model ID (AUMID) for all apps installed for the current user by running the following command in Windows PowerShell command prompt:

get-StartApps

To get AUMIDs for Windows Store apps installed for another user, run the following command in Windows PowerShell command prompt:

$installedapps = get-AppxPackage

$aumidList = @()
foreach ($app in $installedapps)
{
    foreach ($id in (Get-AppxPackageManifest $app).package.applications.application.id)
    {
        $aumidList += $app.packagefamilyname + "!" + $id
    }
}

$aumidList

More detailed information you can refer to " Find the Application User Model ID of an installed app ".

What you need to do is call related registry API or Powershell command from C#.

You get the AppUserModelId with IShellFolder2.GetDetailsEx and PKEY_AppUserModel_ID

(tested in C# on Windows 10)

[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct PROPERTYKEY
{
    private readonly Guid _fmtid;
    private readonly uint _pid;

    public PROPERTYKEY(Guid fmtid, uint pid)
    {
        _fmtid = fmtid;
        _pid = pid;
    }

    public static readonly PROPERTYKEY PKEY_AppUserModel_ID = new PROPERTYKEY(new Guid("9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3"), 5);     
}

Check out this answer.

In short, add the Microsoft.WindowsAPICodePack-Shell nuget package to your project and then use this code:

// GUID taken from https://docs.microsoft.com/en-us/windows/win32/shell/knownfolderid
var FODLERID_AppsFolder = new Guid("{1e87508d-89c2-42f0-8a7e-645a0f50ca58}");
ShellObject appsFolder = (ShellObject)KnownFolderHelper.FromKnownFolderId(FODLERID_AppsFolder);

foreach (var app in (IKnownFolder)appsFolder)
{
    // The friendly app name
    string name = app.Name;
    // The ParsingName property is the AppUserModelID
    string appUserModelID = app.ParsingName; // or app.Properties.System.AppUserModel.ID
    // You can even get the Jumbo icon in one shot
    ImageSource icon =  app.Thumbnail.ExtraLargeBitmapSource;
}

To start the app:

System.Diagnostics.Process.Start("explorer.exe", @" shell:appsFolder\" + appUserModelID);

Works for both regular apps and store apps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM