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

How to check Windows license status in C#?

我希望我的程序检查 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 的许可证状态?

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