Compartilhar via


PackageManager.FindUsers(String) Método

Definição

Localiza os usuários que instalaram o Pacote especificado.

public:
 virtual IIterable<PackageUserInformation ^> ^ FindUsers(Platform::String ^ packageFullName) = FindUsers;
IIterable<PackageUserInformation> FindUsers(winrt::hstring const& packageFullName);
public IEnumerable<PackageUserInformation> FindUsers(string packageFullName);
function findUsers(packageFullName)
Public Function FindUsers (packageFullName As String) As IEnumerable(Of PackageUserInformation)

Parâmetros

packageFullName
String

Platform::String

winrt::hstring

O nome completo do pacote. Esse parâmetro não pode ser nulo.

Retornos

Se o método for bem-sucedido, uma coleção enumerável de objetos de informações do usuário do pacote será retornada. Cada objeto PackageUserInformation nesta coleção contém o SID (identificador de segurança do usuário) de um usuário para o qual o pacote especificado está instalado. Se nenhum pacote com o nome completo do pacote especificado for encontrado, esse método retornará uma lista vazia.

Requisitos do Windows

Funcionalidades do aplicativo
packageManagement

Exemplos

Chame o método PackageManager.FindUsers para enumerar os usuários que instalaram um pacote. Este exemplo exibe as informações na propriedade PackageUserInformation.UserSecurityId .

using System.Security.Principal;
using Windows.Deployment.PackageManager;

private static void DisplayPackageUsers(
    PackageManager packageManager, 
    Windows.ApplicationModel.Package package)
{
    IEnumerable<PackageUserInformation> packageUsers = packageManager.FindUsers(
        package.Id.FullName);

    Console.Write("Users: ");

    foreach (var packageUser in packageUsers)
    {
        Console.Write("{0} ", SidToAccountName(packageUser.UserSecurityId));
    }

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

Confira também Suporte do Visual Studio para C++/WinRT.

// main.cpp : In Visual Studio, create a new Windows Console Application (C++/WinRT), and run it (or run Visual Studio) as administrator.
#include "pch.h"

#include <winrt/Windows.ApplicationModel.h>
#include <winrt/Windows.Management.Deployment.h>
#include <winrt/Windows.Storage.h>
#include <iostream>
#include <Sddl.h>

using namespace winrt;
using namespace Windows::ApplicationModel;
using namespace Windows::Management::Deployment;
using namespace Windows::Storage;

std::wstring SidToAccountName(std::wstring const& sidString)
{
    PSID sid{ nullptr };

    std::wstring accountName{ sidString };

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

        std::wstring name; name.resize(nameCharCount + 1);
        std::wstring domainName; domainName.resize(domainNameCharCount + 1);

        try
        {
            if (::LookupAccountSid(nullptr, sid, name.data(), &nameCharCount, domainName.data(), &domainNameCharCount, &sidType))
            {
                accountName = domainName + L"\\" + name;
            }
        }
        catch (...)
        {
            // do nothing, original SID will be used.
        }
    }

    if (sid != nullptr)
        ::LocalFree(sid);

    return accountName;
}

void DisplayPackageUsers(PackageManager const& packageManager, Windows::ApplicationModel::Package const& package)
{
    std::wcout << L"Users: ";

    for (auto const& packageUser : packageManager.FindUsers(package.Id().FullName()))
    {
        std::wstring stringSid = SidToAccountName(packageUser.UserSecurityId().c_str());

        std::wcout << stringSid << L" ";
    }
    std::wcout << std::endl;
}

int wmain()
{
    winrt::init_apartment();

    PackageManager packageManager;

    int count{ 10 };
    for (auto const& package : packageManager.FindPackages())
    {
        DisplayPackageUsers(packageManager, package);
        count--;
        if (count == 0) break;
    }

    return 0;
}
using Windows::Management::Deployment;

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

    wcout << L"Users: ";

    std::for_each(begin(packageUsers), end(packageUsers), 
        [&](PackageUserInformation^ packageUser)
    {
        wstring stringSid;

        SidToAccountName(packageUser->UserSecurityId->Data(), stringSid);

        wcout << stringSid << L" ";
    });
    wcout << endl;
}

void SidToAccountName(wstring sidString, wstring& stringSid)
{
    PSID sid = NULL;

    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(NULL, sid, NULL, &nameCharCount, NULL, &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));

        try
        {
            if ( LookupAccountSid(NULL, sid, name, &nameCharCount, domainName, &domainNameCharCount, &sidType) )
            {
                stringSid = domainName;
                stringSid = stringSid + L"\\" + name;
            }
        }
        catch ( ... )
        {
            // do nothing, original SID will be used.
        }

        delete [] domainName;
        delete [] name;
    }

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

    if ( sid != NULL )
        LocalFree(sid);
}

Comentários

Esse método requer privilégios administrativos.

Aplica-se a

Confira também