如何检查我的.NET应用程序是否在终端服务器上运行?

时间:2016-09-14 10:05:55

标签: c# .net

我有一个.net应用程序,我需要弄清楚我的应用程序是否在启用了终端服务的Windows设备上运行。如何在c#中执行此操作?

编辑:RDP服务器也可以被带到非服务器类机器,如Windows 7.并且服务器类机器也可以在RDP中,而不启用终端服务。

1 个答案:

答案 0 :(得分:0)

您可以查询WMI Win32_TerminalServiceSetting课程以获取此信息。见这个例子:

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

参考文献:

MSDN: Win32_TerminalServiceSetting class

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