Condividi tramite


Rilevamento del fatto che il ruolo Servizi Desktop remoto sia installato

È possibile usare la classe WMI Win32_ServerFeature per rilevare se è installato il ruolo del server Servizi Desktop remoto.

Nell'esempio C# seguente viene illustrato un metodo che restituisce True se il ruolo del server Servizi Desktop remoto è installato ed è in esecuzione o false in caso contrario. Poiché la classe WMI Win32_ServerFeature è disponibile solo a partire da Windows Server 2008, questo codice non è compatibile con le versioni precedenti di 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;
}