Reputation: 174
How to query all PC's in the network to see who's actually logged on. I need to get list of "IPAddress + LogedUserName".
I am getting the list of computers that are available on LAN by this way:
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";
}
}
}
}
But I want to have also the name of logged user on each available computer.
Upvotes: 3
Views: 2759
Reputation: 127603
Now that your question was re-opened here is a more full explanation of using cassia. This may or may not work on desktops that do not have RemoteDesktop enabled. This code is totally untested, it likely needs some fixes before it will work 100%, but it will get you on the right track.
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;
}
}
}
}
Upvotes: 1