gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:44:32 25 4
gpt4 key购买 nike

我想让我的程序检查 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 类 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# - 如何在 C# 中检查 Windows 许可证状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39231105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com