How to check Windows license status in C#?

99封情书 提交于 2019-12-22 18:35:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!