检测是否已安装远程桌面服务角色
可以使用 Win32_ServerFeature WMI 类来检测是否已安装远程桌面服务服务器角色。
下面的 C# 示例演示了一个方法,该方法在已安装并运行远程桌面服务服务器角色时返回 True ,否则返回 false 。 由于 Win32_ServerFeature WMI 类仅从 Windows Server 2008 开始可用,因此此代码与早期版本的 Windows 不兼容。
static void Main(string[] args)
{
// 14 is the identifier of the Remote Desktop Services role.
HasServerFeatureById(14);
}
static bool HasServerFeatureById(UInt32 roleId)
{
try
{
ManagementClass serviceClass = new ManagementClass("Win32_ServerFeature");
foreach (ManagementObject feature in serviceClass.GetInstances())
{
if ((UInt32)feature["ID"] == roleId)
{
return true;
}
}
return false;
}
catch (ManagementException)
{
// The most likely cause of this is that this is being called from an
// operating system that is not a server operating system.
}
return false;
}