Get installed applications in a system

后端 未结 14 1024
遥遥无期
遥遥无期 2020-11-22 08:28

How to get the applications installed in the system using c# code?

14条回答
  •  悲哀的现实
    2020-11-22 09:20

    I wanted to be able to extract a list of apps just as they appear in the start menu. Using the registry, I was getting entries that do not show up in the start menu.

    I also wanted to find the exe path and to extract an icon to eventually make a nice looking launcher. Unfortunately, with the registry method this is kind of a hit and miss since my observations are that this information isn't reliably available.

    My alternative is based around the shell:AppsFolder which you can access by running explorer.exe shell:appsFolder and which lists all apps, including store apps, currently installed and available through the start menu. The issue is that this is a virtual folder that can't be accessed with System.IO.Directory. Instead, you would have to use native shell32 commands. Fortunately, Microsoft published the Microsoft.WindowsAPICodePack-Shell on Nuget which is a wrapper for the aforementioned commands. Enough said, here's the code:

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

    And that's all there is to it. You can also start the apps using

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

    This works for regular Win32 apps and UWP store apps. How about them apples.

    Since you are interested in listing all installed apps, it is reasonable to expect that you might want to monitor for new apps or uninstalled apps as well, which you can do using the ShellObjectWatcher:

    ShellObjectWatcher sow = new ShellObjectWatcher(appsFolder, false);
    sow.AllEvents += (s, e) => DoWhatever();
    sow.Start();
    

    Edit: One might also be interested in knowing that the AppUserMoedlID mentioned above is the unique ID Windows uses to group windows in the taskbar.

提交回复
热议问题