在我的应用程序中,我想查看是否激活了Windows 7。 要清楚,我不想检查窗口是否真实。 我使用下面的代码,在这里找到http://www.dreamincode.net/forums/topic/166690-wmi-softwarelicensingproduct/减少WMI查询执行时间
执行查询所需的时间约为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;
}
都能跟得上。多数民众赞成在与wmi做到这一点。没有太多的工作要做得更快。 – 2012-02-01 19:42:34