PackageManager.FindUsers(String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
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)
參數
- packageFullName
-
String
Platform::String
winrt::hstring
封裝的完整名稱。 此參數不可為 Null。
傳回
如果方法成功,則會傳回封裝使用者資訊物件的可列舉集合。 此集合中的每個 PackageUserInformation 物件都包含使用者安全識別碼 (使用者 SID) 安裝指定套件的使用者。 如果找不到具有指定套件完整名稱的套件,這個方法會傳回空的清單。
Windows 需求
應用程式功能 |
packageManagement
|
範例
呼叫 PackageManager.FindUsers 方法來列舉已安裝套件的使用者。 本範例會顯示 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;
}
}
另請參閱 C++/WinRT 的 Visual Studio 支援。
// 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);
}
備註
這個方法需要系統管理許可權。