0

I have a .net application where I need to figure out whether my application is running on a windows device with terminal service enabled.How can I do that in c#?

EDIT: RDP server can also be taken to non-server class machines like windows 7. And Server class machines can also be in RDP without terminal services being enabled.

  • Well you could check if the current user is logged in via rdp - but if your app is a service that wouldnt work – BugFinder Sep 14 '16 at 10:09
  • Possible duplicate of [How do I tell if my application is running in an RDP session](http://stackoverflow.com/questions/295415/how-do-i-tell-if-my-application-is-running-in-an-rdp-session) – Avner Shahar-Kashtan Sep 14 '16 at 10:10
  • In most cases, just have a function that returns `true`. Fast User Switching (which is enabled on most client machines) is implemented using TS. Every server supports a (limited number) of TS sessions. Basically, almost every machine is running TS. – Damien_The_Unbeliever Sep 14 '16 at 10:10
  • That will not help.I need to make sure its a terminal server.RDP can also be taken for non-terminal server machines. – Somesh Dhal Sep 14 '16 at 10:12
  • Look at this: http://stackoverflow.com/questions/10013205/how-to-know-if-wpf-app-is-in-terminal-services-session – Gabsch Sep 14 '16 at 10:28
  • @SomeshDhal A "TS session" *is* an RDP session. Why do you want to know if TS is involved? What is the *real* problem you are trying to solve? – Panagiotis Kanavos Sep 14 '16 at 10:29

1 Answers1

1

You could query WMI Win32_TerminalServiceSetting class for this information. See this example:

using System.Management;
//...
//create a management scope object
ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\CIMV2\\TerminalServices");

//create object query
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_TerminalServiceSetting");

//create object searcher
ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher(scope, query);

//get a collection of WMI objects
ManagementObjectCollection queryCollection = searcher.Get();

//enumerate the collection.
foreach (ManagementObject m in queryCollection) 
{
  // access properties of the WMI object
  Console.WriteLine("Terminal server enabled : {0}", m["AllowTSConnections"]);
}

References:

MSDN: Win32_TerminalServiceSetting class

How Can I Determine Whether Terminal Services is Enabled on a Windows Server 2003 Computer?

Ari0nhh
  • 5,720
  • 3
  • 28
  • 33