backslash17
backslash17

Reputation: 5390

How to detect since when a user is logged into the system with .NET (C#)?

I need to detect the logged on time of a user connected to the server by remote desktop or console in C#. I was trying to search a property in the WMI classes but I did not find any. Thanks again in advance!

Upvotes: 3

Views: 954

Answers (1)

Adam Hughes
Adam Hughes

Reputation: 3812

Check out cassia, the .NET Terminal Services Library.

ITerminalServicesManager manager = new TerminalServicesManager();
using (ITerminalServer server = manager.GetRemoteServer("your-server-name"))
{
    server.Open();
    foreach (ITerminalServicesSession session in server.GetSessions())
    {
        Console.WriteLine("Session ID: " + session.SessionId);
        Console.WriteLine("User: " + session.UserAccount);
        Console.WriteLine("State: " + session.ConnectionState);
        Console.WriteLine("Logon Time: " + session.LoginTime);
    }
}

You could also use P/Invoke to access the Windows Terminal Services API directly, but cassia wraps it for you.

Upvotes: 2

Related Questions