vandervagos
vandervagos

Reputation: 113

Decrease WMI query execution time

In my application i want to see if windows 7 is activated. To be clear, i do not want to check if windows are genuine. I use the code below, found here http://www.dreamincode.net/forums/topic/166690-wmi-softwarelicensingproduct/

The time needed to execute the query is about 5-10 sec. Is there anyway to reduce the time needed? Or another way to check if winows 7 is activated?

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;

}

Upvotes: 4

Views: 3152

Answers (3)

Alphawaves
Alphawaves

Reputation: 1

This i made is quick :)

 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;
    }

Its usage:

if(IsLicenced())
            MessageBox.Show("Windows is Licensed");

Upvotes: 0

Hans
Hans

Reputation: 13030

I would recommend querying only the properties you really need. So, if you only need the LicenseStatus value of the SoftwareLicensingProduct WMI class then use the following query:

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

This should improve your performance. As DJ KRAZE pointed out in his answer you should of course dispose your management classes.

On my Windows 7 machine using only the LicenseStatus property in the query took 246ms. Querying for all properties (using the "*") took 2440ms.

Upvotes: 6

MethodMan
MethodMan

Reputation: 18843

This is generally the way WMI works it's querying at least.. where you have the following below .. after your foreach I would dispose of those objects ..

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

if they implement IDisposeable then you could do

((IDisposable)scope).Dispose();
((IDisposable)searchQuery).Dispose();
((IDisposable)searcherObj).Dispose();

if not then do an if() to check if the object != null then Dispose of them individually Try running this several times and seeing if it returns faster or not once you dispose of the objects.. other than that.. not much you can do from what it looks like to make it faster..

Upvotes: 2

Related Questions