PackageManager.FindPackageForUser(String, String)

2020-05-01 04:37发布

问题:

Why my following code example of this method is returning?

using Windows.Management.Deployment;
…
...
Windows.ApplicationModel.Package oPkg = oPkgManager.FindPackageForUser(string.Empty, "HoloCamera_1.0.0.5_neutral__cw5n1h2txyewy");

Remark: To test the FindPackageForUser(…) method, you will need to first add following references to your VS2017 project of any type (Winform, WPF, etc) as explained here:

  1. C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.17763.0\Windows.winmd
  2. C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll

NOTE: First using VS2017 , I ran this sample code example for FindPackages() method to find all the packages installed on my Windows 10. And I found out several packages that are installed on windows by default. And, I tried the following two but both of them return null on the above code line.

Following are two of the packages that FindPackages() method returns. And, I tried both of them in my above code example:

1.

Name: HoloCamera
FullName: HoloCamera_1.0.0.5_neutral__cw5n1h2txyewy

Version: 1.0.0.5

Publisher: CN=Microsoft Windows, O=Microsoft Corporation, L=Redmond, S=Washington, C=US

PublisherId: cw5n1h2txyewy

IsFramework: False

And

2.

Name: DesktopLearning

FullName: DesktopLearning_1000.15063.0.0_neutral__cw5n1h2txyewy

Version: 1000.15063.0.0

Publisher: CN=Microsoft Windows, O=Microsoft Corporation, L=Redmond, S=Washington, C=US

PublisherId: cw5n1h2txyewy

IsFramework: False

回答1:

There's no problem with the FindPackageForUser method. To make this method work successfully, you first need to run your visual studio in Administrator mode(Steps: 'Start-> right click your visual studio -> More -> Run as Administrator').

Then, when you call FindPackageForUser method and pass the string.Empty as the first param, if it return NULL, it means that this package is not installed for current user.

To validate this point, you could check the message in Output window when you call FindPackages() method. The different packages should have different user and user securityId. You could use the 'DisplayPackageUsers' method to see the user securityId like the following:

private static void DisplayPackageUsers(Windows.Management.Deployment.PackageManager packageManager, Windows.ApplicationModel.Package package)
{
    IEnumerable<Windows.Management.Deployment.PackageUserInformation> packageUsers = packageManager.FindUsers(package.Id.FullName);
    Debug.Write("Users: ");
    foreach (var packageUser in packageUsers)
    {
        Debug.Write(string.Format("{0},UserSecurityId: {1} ", SidToAccountName(packageUser.UserSecurityId), packageUser.UserSecurityId));
    }
    Debug.WriteLine("");
}
private static string SidToAccountName(string sidString)
{
    SecurityIdentifier sid = new SecurityIdentifier(sidString);
    try
    {
        NTAccount account = (NTAccount)sid.Translate(typeof(NTAccount));
        return account.ToString();
    }
    catch (IdentityNotMappedException)
    {
        return sidString;
    }
}

So, if you want to use FindPackageForUser method to find some one package, you also need to pass specific user securityId as the first param. You could get the appropriate user securityId from the above methods. Then, calling the FindPackageForUser method will return the specific package information successfully.



标签: c# winforms uwp