如何在C#中检查Windows许可证状态?

时间:2016-08-30 15:06:15

标签: c# windows activation

我希望我的程序检查Windows 10是否已被激活

我有以下代码

 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;
    }

尝试使用此代码时,调试器会抱怨Invalid class,我不知道它是什么

我该怎么做才能解决这个问题?或者有没有其他方法来检查Windows的许可证状态?

1 个答案:

答案 0 :(得分:7)

仅在Windows XP上支持WMI类Win32_WindowsProductActivation。对于Windows 10,您需要使用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;
    }
}