PackageManager.FindPackageForUser(String, String) 总是返回 null [英] PackageManager.FindPackageForUser(String, String) always returning null

查看:24
本文介绍了PackageManager.FindPackageForUser(String, String) 总是返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的以下代码示例 this 方法正在返回?

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");

备注:要测试 FindPackageForUser(…) 方法,您需要先添加以下对您的 VS2017 项目的任何类型的引用(Winform、WPF 等)如解释 此处:

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

注意:首先使用 VS2017 ,我运行了 this 示例代码示例,用于 FindPackages() 方法以查找安装在我的 Windows 10 上的所有软件包代码>.我发现了几个默认安装在 Windows 上的软件包.而且,我尝试了以下两个,但它们都在上面的代码行中返回 null.

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.

以下是 FindPackages() 方法返回.而且,我在上面的代码示例中尝试了它们:

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

推荐答案

FindPackageForUser 方法没有问题.要使此方法成功运行,您首先需要在 管理员 模式下运行您的 Visual Studio(步骤:'开始-> 右键单击​​您的 Visual Studio -> 更多 -> 以管理员身份运行').

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').

然后,当你调用FindPackageForUser 方法并传入string.Empty 作为第一个参数时,如果返回NULL,则表示未为当前用户安装此软件包.

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.

要验证这一点,您可以在调用 FindPackages() 方法时检查输出窗口中的消息.不同的包应该有不同的用户和用户 securityId.您可以使用DisplayPackageUsers"方法查看用户 securityId,如下所示:

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;
    }
}

因此,如果您想使用 FindPackageForUser 方法查找某个包,您还需要将特定用户 securityId 作为第一个参数传递.您可以从上述方法中获取适当的用户 securityId.然后调用FindPackageForUser方法会成功返回具体的包信息.

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.

这篇关于PackageManager.FindPackageForUser(String, String) 总是返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆