1

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.

Amicable
  • 3,013
  • 3
  • 46
  • 75
Mike Johanson
  • 61
  • 2
  • 9

2 Answers2

1

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
}
Damien_The_Unbeliever
  • 227,877
  • 22
  • 326
  • 423
0

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.

Community
  • 1
  • 1
Remus Rusanu
  • 281,117
  • 39
  • 423
  • 553
  • thanks but the easier way gets these results : \\Computername\root\cimv2:Win32_LogonSession.LogonId="995" \\Computername\root\cimv2:Win32_LogonSession.LogonId="999" \\Computername\root\cimv2:Win32_LogonSession.LogonId="997" \\Computername\root\cimv2:Win32_LogonSession.LogonId="996" \\Computername\root\cimv2:Win32_LogonSession.LogonId="146406" \\Computername\root\cimv2:Win32_LogonSession.LogonId="224478" \\Computername\root\cimv2:Win32_LogonSession.LogonId="709133" not the things i want to get. by the way thanks for your effort. – Mike Johanson Mar 18 '14 at 16:05