簡體   English   中英

減少WMI查詢執行時間

[英]Decrease WMI query execution time

在我的應用程序中,我想看看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;

}

我建議只查詢你真正需要的屬性。 因此,如果您只需要SoftwareLicensingProduct WMI類的LicenseStatus值,請使用以下查詢:

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

這應該可以改善您的表現。 正如DJ KRAZE在答案中指出的那樣,你當然應該處理你的管理課程。

在我的Windows 7機器上僅使用查詢中的LicenseStatus屬性花了246ms 查詢所有屬性(使用“*”)需要2440毫秒

這通常是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然后單獨處理它們嘗試運行這幾次,看看它處理對象后是否返回更快..除此之外..不是很多你可以做到它看起來更快..

這我很快:)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM