简体   繁体   中英

c# check logged users on network

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM