c # проверка зарегистрированных пользователей в сети - PullRequest
3 голосов
/ 20 октября 2011

Как запросить все ПК в сети, чтобы узнать, кто на самом деле вошел в систему.Мне нужно получить список «IPAddress + LogedUserName».

Я получаю список компьютеров, доступных в локальной сети, следующим образом:

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

Но я хочу иметь такжеимя зарегистрированного пользователя на каждом доступном компьютере.

1 Ответ

1 голос
/ 15 ноября 2011

Теперь, когда ваш вопрос был вновь открыт, приведено более полное объяснение использования 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;
            }
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...