jm33_m0 jm33_m0 - 3 months ago 19
C# Question

How to check Windows license status in C#?

I want my program to check if Windows 10 has been activated

I have the following code


public static bool IsWindowsActivated()
{
bool activated = true;
ManagementScope scope = new ManagementScope(@"\\" + System.Environment.MachineName + @"\root\cimv2");
scope.Connect();

SelectQuery searchQuery = new SelectQuery("SELECT * FROM Win32_WindowsProductActivation");
ManagementObjectSearcher searcherObj = new ManagementObjectSearcher(scope, searchQuery);

using (ManagementObjectCollection obj = searcherObj.Get())
{
foreach (ManagementObject o in obj)
{
activated = ((int)o["ActivationRequired"] == 0) ? true : false;
}
}
return activated;
}


when trying to use this code, the debugger complains
Invalid class
, which I have no idea what it is

what should I do to fix this? or is there any other way to check the license status of Windows?

Answer

The WMI class Win32_WindowsProductActivation is only supported on windows XP. For windows 10 you need to use SoftwareLicensingProduct

public static bool IsWindowsActivated()
{
    ManagementScope scope = new ManagementScope(@"\\" + System.Environment.MachineName + @"\root\cimv2");
    scope.Connect();

    SelectQuery searchQuery = new SelectQuery("SELECT * FROM SoftwareLicensingProduct WHERE ApplicationID = '55c92734-d682-4d71-983e-d6ec3f16059f' and LicenseStatus = 1");
    ManagementObjectSearcher searcherObj = new ManagementObjectSearcher(scope, searchQuery);

    using (ManagementObjectCollection obj = searcherObj.Get())
    {
        return obj.Count > 0;
    }
}