在我的应用程序中,我想看看Windows 7是否已激活。 要清楚,我不想检查窗户是否是正品。 我使用下面的代码http://www.dreamincode.net/forums/topic/166690-wmi-softwarelicensingproduct/
执行查询所需的时间约为5-10秒。反正有没有减少所需的时间?或者另一种检查winows 7是否被激活的方法?
public string VistaOrNewerStatus(){
string status = string.Empty;
string computer = ".";
try
{
//set the scope of this search
ManagementScope scope = new ManagementScope(@"\\" + computer + @"\root\cimv2");
//connect to the machine
scope.Connect();
//use a SelectQuery to tell what we're searching in
SelectQuery searchQuery = new SelectQuery("SELECT * FROM SoftwareLicensingProduct");
//set the search up
ManagementObjectSearcher searcherObj = new ManagementObjectSearcher(scope, searchQuery);
//get the results into a collection
using (ManagementObjectCollection obj = searcherObj.Get())
{
MessageBox.Show(obj.Count.ToString());
//now loop through the collection looking for
//an activation status
foreach (ManagementObject o in obj)
{
//MessageBox.Show(o["ActivationRequired"].ToString());
switch ((UInt32)o["LicenseStatus"])
{
case 0:
status = "Unlicensed";
break;
case 1:
status = "Licensed";
break;
case 2:
status = "Out-Of-Box Grace Period";
break;
case 3:
status = "Out-Of-Tolerance Grace Period";
break;
case 4:
status = "Non-Genuine Grace Period";
break;
}
}
}
// return activated;
}
catch (Exception ex)
{
// MessageBox.Show(ex.ToString());
status = ex.Message;
//return false;
}
return status;
}
答案 0 :(得分:6)
我建议只查询你真正需要的属性。因此,如果您只需要LicenseStatus
WMI类的SoftwareLicensingProduct
值,请使用以下查询:
SelectQuery searchQuery = new
SelectQuery("SELECT LicenseStatus FROM SoftwareLicensingProduct");
这可以改善您的表现。正如DJ KRAZE在答案中指出的那样,你当然应该处理你的管理课程。
在我的Windows 7计算机上,仅使用查询中的LicenseStatus属性 246ms 。查询所有属性(使用“*”)需要 2440ms 。
答案 1 :(得分:2)
这通常是WMI工作的方式,它至少要查询..在下面你有以下内容..在你的foreach之后我将处理这些对象..
ManagementScope scope = new ManagementScope(@"\\" + computer + @"\root\cimv2");
//connect to the machine
scope.Connect();
//use a SelectQuery to tell what we're searching in
SelectQuery searchQuery = new SelectQuery("SELECT * FROM SoftwareLicensingProduct");
//set the search up
ManagementObjectSearcher searcherObj
如果他们实现IDisposeable,那么你可以做
((IDisposable)scope).Dispose();
((IDisposable)searchQuery).Dispose();
((IDisposable)searcherObj).Dispose();
如果没有,则执行if()以检查对象是否!= null然后单独处理它们 尝试运行这几次,看看它是否会在处理完对象后返回得更快......除此之外......你可以做的不多,看起来更快......
答案 2 :(得分:0)
我做的很快:)
public bool IsLicensed(bool Licensed = false)
{
try
{
foreach (ManagementObject Obj in new ManagementObjectSearcher("root\\CIMV2", "SELECT LicenseStatus FROM SoftwareLicensingProduct WHERE LicenseStatus = 1").Get())
{
Licensed = true;
}
}
catch (ManagementException) { Licensed = false; }
return Licensed;
}
用法:
if(IsLicenced())
MessageBox.Show("Windows is Licensed");