问题
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?
回答1:
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;
}
}
来源:https://stackoverflow.com/questions/39231105/how-to-check-windows-license-status-in-c