1. 程式人生 > >win10 查詢 uwp 安裝包 資訊

win10 查詢 uwp 安裝包 資訊

PS C:\Users\Mr.wang> Get-AppxPackage *MicrosoftEdge


Name              : Microsoft.MicrosoftEdge
Publisher         : CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US
Architecture      : Neutral
ResourceId        :
Version           : 44.17763.1.0
PackageFullName   : Microsoft.MicrosoftEdge_44.17763.1.0_neutral__8wekyb3d8bbwe
InstallLocation   : C:\Windows\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe
IsFramework       : False
PackageFamilyName : Microsoft.MicrosoftEdge_8wekyb3d8bbwe
PublisherId       : 8wekyb3d8bbwe
IsResourcePackage : False
IsBundle          : False
IsDevelopmentMode : False
NonRemovable      : True
IsPartiallyStaged : False
SignatureKind     : System
Status            : Ok

Get-AppxPackage 是powershell 下的一個工具,可以用來管理 uwp 包。
上面的用例 顯示了 查詢 MicrosoftEdge 包資訊的方法。

microsoft 官方文件給出了一個 c++ 查詢 uwp 包資訊的例子:

//// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//// PARTICULAR PURPOSE.
////
//// Copyright (c) Microsoft Corporation. All rights reserved

#include <windows.h>
#include <sddl.h>

#include <algorithm>
#include <collection.h>
#include <iostream>
#include <string>

#using <Windows.winmd>

using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace std;

void DisplayPackageInfo(Windows::ApplicationModel::Package^ package)
{
    wcout << L"Name: " << package->Id->Name->Data() << endl;
    wcout << L"FullName: " << package->Id->FullName->Data() << endl;
    wcout << L"Version: " << package->Id->Version.Major << "." << 
        package->Id->Version.Minor << "." << package->Id->Version.Build << 
        "." << package->Id->Version.Revision << endl;
    wcout << L"Publisher: " << package->Id->Publisher->Data() << endl;
    wcout << L"PublisherId: " << package->Id->PublisherId->Data() << endl;
    wcout << L"Installed Location: " << package->InstalledLocation->Path->Data() << endl;
    wcout << L"IsFramework: " << (package->IsFramework ? L"True" : L"False") << endl;
}

void SidToAccountName(wstring const &sidString, wstring& stringSid)
{
    PSID sid = nullptr;
    if (ConvertStringSidToSid(sidString.c_str(), &sid))
    {
        DWORD nameCharCount = 0;
        DWORD domainNameCharCount = 0;
        SID_NAME_USE sidType;

        // determine how much space is required to store the name and domainName
        LookupAccountSid(nullptr, sid, nullptr, &nameCharCount, nullptr, &domainNameCharCount, &sidType);

        wchar_t *name = new wchar_t[nameCharCount + 1]; // leave space for terminating null
        wchar_t *domainName = new wchar_t[domainNameCharCount + 1];

        ZeroMemory(name, (nameCharCount + 1) * sizeof(wchar_t));
        ZeroMemory(domainName, (domainNameCharCount + 1) * sizeof(wchar_t));

        if (LookupAccountSid(nullptr, sid, name, &nameCharCount, domainName, &domainNameCharCount, &sidType))
        {
            stringSid = domainName;
            stringSid = stringSid + L"\\" + name;
        }

        delete [] domainName;
        delete [] name;
        LocalFree(sid);
    }

    if (stringSid.length() == 0)
        stringSid = sidString;
}

void DisplayPackageUsers(Windows::Management::Deployment::PackageManager^ packageManager, Windows::ApplicationModel::Package^ package)
{
    IIterable<Windows::Management::Deployment::PackageUserInformation^>^ packageUsers = packageManager->FindUsers(package->Id->FullName);

    wcout << L"Users: ";

    std::for_each(begin(packageUsers), end(packageUsers), [](Windows::Management::Deployment::PackageUserInformation^ packageUser)
    {
        wstring stringSid;
        SidToAccountName(packageUser->UserSecurityId->Data(), stringSid);
        wcout << stringSid << L" ";
    });

    wcout << endl;
}

[STAThread]
int __cdecl main(Platform::Array<String^>^ args)
{
    wcout << L"Copyright (c) Microsoft Corporation. All rights reserved." << endl;
    wcout << L"FindPackagesByNameAndPublisher sample" << endl << endl;

    if (args->Length < 3)
    {
        wcout << L"Usage: FindPackagesByNameAndPublisherSample.exe packageName publisherName";
        return 1;
    }

    String^ inputPackageName = args[1];
    String^ inputPublisherName = args[2];

    try
    {
        auto packageManager = ref new Windows::Management::Deployment::PackageManager();
        auto packages = packageManager->FindPackages(inputPackageName, inputPublisherName);

        int packageCount = 0;
        std::for_each(Windows::Foundation::Collections::begin(packages), Windows::Foundation::Collections::end(packages), 
            [&packageManager, &packageCount](Windows::ApplicationModel::Package^ package)
        {
            DisplayPackageInfo(package);
            DisplayPackageUsers(packageManager, package);

            wcout << endl;
            packageCount += 1;
        });

        if (packageCount < 1)
        {
            wcout << L"No packages were found." << endl;
        }
    }
    catch (AccessDeniedException^)
    {
        wcout << L"FindPackagesByNameAndPublisherSample failed because access was denied. This program must be run from an elevated command prompt." << endl;
        return 1;
    }
    catch (Exception^ ex)
    {
        wcout << L"FindPackagesByNameAndPublisher failed, error message: " << ex->ToString()->Data() << endl;
        return 1;
    }

    return 0;
}

注意上面的程式碼需要管理員許可權執行