如何在 C# 中检查 Windows 许可证状态? [英] How to check Windows license status in C#?

查看:25
本文介绍了如何在 C# 中检查 Windows 许可证状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我有以下代码

 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,我不知道它是什么

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

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

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

推荐答案

WMI 类 Win32_WindowsProductActivation 仅在 Windows XP 上受支持.对于 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;
    }
}

这篇关于如何在 C# 中检查 Windows 许可证状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆