gpt4 book ai didi

c#检查网络上登录的用户

转载 作者:行者123 更新时间:2023-11-30 18:02:49 25 4
gpt4 key购买 nike

如何查询网络中的所有 PC 以查看实际登录的用户。我需要获取“IPAddress + LogedUserName”列表。

我正在通过这种方式获取 LAN 上可用的计算机列表:

using (DirectoryEntry root = new DirectoryEntry("WinNT:"))
{
foreach (DirectoryEntry computers in root.Children)
{
foreach (DirectoryEntry computer in computers.Children)
{
if ((computer.Name != "Schema"))
{
textBox1.Text += computer.Name + "\r\n";
}
}
}
}

但我还想在每台可用计算机上拥有登录用户的名称。

最佳答案

既然你的问题在这里被重新提出来了,那么关于使用 cassia 的更完整的解释.这可能适用于也可能不适用于未启用 RemoteDesktop 的桌面。此代码完全未经测试,可能需要一些修复才能 100% 运行,但它会让您走上正轨。

using (DirectoryEntry root = new DirectoryEntry("WinNT:"))
{
ITerminalServicesManager tsm = new Cassia.TerminalServicesManager();

foreach (DirectoryEntry computers in root.Children)
foreach (DirectoryEntry computer in computers.Children)
{
if ((computer.Name != "Schema"))
{
string linqCapture = computer.Name; //<-- This may not be necessary,
//but I have always have had bad
//experiences with LINQ and foreach
//loops not capturing the current
//value of the variable correctly.

//remove the last Where clause if you want all users connected
//to the computer, not just the one where it is the console session.
foreach(var session in tsm.GetSessions(linqCapture)
.Where(s => s.ConnectionState == ConnectionState.Active)
.Where(s => s.ClientName == linqCapture))
{
string LoggedInUser = session.UserName;
System.Net.IPAddress LoggedInIp = session.ClientIPAddress;
//Do with data what ever you want to;
}
}
}
}

关于c#检查网络上登录的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7828376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com