2012-02-01 117 views
3

在我的应用程序中,我想查看是否激活了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; 

}

+0

都能跟得上。多数民众赞成在与wmi做到这一点。没有太多的工作要做得更快。 – 2012-02-01 19:42:34

回答

6

我会建议只查询你真正需要的属性。所以,如果你只需要SoftwareLicensingProduct WMI类的LicenseStatus值,那么请使用以下查询:

SelectQuery searchQuery = new 
      SelectQuery("SELECT LicenseStatus FROM SoftwareLicensingProduct"); 

这应该提高你的表现。正如DJ KRAZE在他的回答中指出的那样,你当然应该处理你的管理课程。

在我的Windows 7机器上只使用查询中的LicenseStatus属性花了246ms。查询所有属性(使用“*”)需要2440ms

+0

感谢您认识汉斯+2给您现在.. – MethodMan 2012-02-01 20:52:02

+0

这个答案与下面的答案相结合,显着减少了时间! Thx很多家伙! – vandervagos 2012-02-01 20:52:53

+0

不是vandervagos的问题..在这里,所有关于学习和传递知识和经验。 – MethodMan 2012-02-01 20:54:48

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(); 

如果没有那么做的,如果()检查如果对象!= null,那么单独处理它们 尝试运行这几次,并看到它是否返回更快或一旦你处置的对象..除此之外..没有太多,你可以做它看起来像做什么它更快..

+0

我的问题是性能。我可以尝试处理你建议的对象,但是由于我对每个对象都执行了查询,所以不会更快。 在我的理解ManagementObjectCollection obj = searcherObj.Get()函数负责耗时 – vandervagos 2012-02-01 20:11:35

+0

我明白,但我想知道如果你运行一次,然后重新开始多次..你是真正处理创建的对象或他们是否坐在托盘等待被GC释放看看这个链接以及一些可能的性能计数器,你可以设置..看看页面的底部http://technet.microsoft.com/en -us/library/ee692772.aspx#EBPAC – MethodMan 2012-02-01 20:13:36

+0

+1为您的答案。处理对象总是一个好主意 - 不仅仅是出于性能原因。 – Hans 2012-02-01 21:00:27

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");