4

在我的应用程序中,我想查看 Windows 7 是否已激活。需要明确的是,我不想检查 windows 是否是正版的。我使用下面的代码,在这里找到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;

}

4

3 回答 3

6

我建议只查询您真正需要的属性。因此,如果您只需要 WMI 类的LicenseStatus值,请SoftwareLicensingProduct使用以下查询:

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

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

在我的 Windows 7 机器上,在查询中仅使用 LicenseStatus 属性花费了246ms。查询所有属性(使用“*”)花费了2440 毫秒

于 2012-02-01T20:25:42.297 回答
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 然后分别处理它们尝试运行几次,看看它是否在处理对象后返回更快..除此之外..你不多可以从看起来使它更快..

于 2012-02-01T19:52:41.540 回答
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");
于 2013-02-03T23:21:07.310 回答