Mike Johanson
Mike Johanson

Reputation: 61

How to find the session name of the logged in Windows user?

As you can see in the tab user of the windows task manager, there are 5 columns :

User   ID   Status  Client Name  Session
Mike   1    Active               Console

I have used this to get the session id :

System.Diagnostics.Process.GetCurrentProcess().SessionId.ToString();

I want to know the session name to see if it is console or remote desktop or etc.

private string getsessionname()
{
  // function to get session name
}

if(getsessionname=="console")
{
  // do staff1
}
else
{
  // do staff2
}

thanks.

Upvotes: 1

Views: 3533

Answers (2)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239636

Rather than getting the session name and then testing for certain values, are you not just looking for SystemInformation.TerminalServerSession?

Gets a value indicating whether the calling process is associated with a Terminal Services client session.

E.g.:

using System.Windows.Forms;

...

if(SystemInformation.TerminalServerSession)
{
  // do stuff where the user is using remote desktop
}
else
{
  // user is connected locally, e.g. the console
}

Upvotes: 1

Remus Rusanu
Remus Rusanu

Reputation: 294197

There is no managed API. One way is to use the native API via P-Invoke, see Retrieving Logon Session Information. But a much easier way is to use WMI and query the Win32_LogonSession object.

Upvotes: 0

Related Questions