是否可以使用System.DirectoryServices.AccountManagement查找用户当前登录到特定PC的用户?
使用PrincipalSearcher可以轻松找到域中的所有计算机,甚至可以在上次登录时找到它们。但是,我找不到一种简单的方法来查找登录到pcs的用户的用户名。
关于如何做到这一点的任何想法?
答案 0 :(得分:1)
要查找当前登录的用户,您需要知道从用户帐户运行的进程。因此,对于每台PC,您需要获取进程列表并查找执行进程的帐户。为此,您可以使用WMI。
const string ns = @"root\cimv2";
var host = "your host";
var scope = new ManagementScope(string.Format(@"\\{0}\{1}", host, ns));
scope.Connect();
//List of logged in users
using (var searcher = new ManagementObjectSearcher(scope,
new ObjectQuery("select * from Win32_LoggedOnUser")))
{
foreach (var logonUser in searcher.Get())
{
//see MSDN for available properties of Win32_LoggedOnUser,
//take into consideration "Dependent", "Antecedent" properties
}
}
//list of processes
using (var searcher = new ManagementObjectSearcher(scope,
new ObjectQuery( "select * from Win32_SessionProcess")))
{
foreach (var sessProc in searcher.Get())
{
//see "Antecedent" property
}
}