通过SoftwareProductLicensing类查找Windows激活的问题

时间:2012-10-17 23:21:59

标签: c#-4.0 wmi wmi-query

我使用以下代码来确定Widnows7的激活。我得到了7个实例/产品。我不清楚哪个产品是指原始的Windows 7。

我无法找到有关确定Windows是否已激活的实例的文档

        //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())
        {
            foreach (ManagementObject m in obj)
        {
            if (Convert.ToInt32(m["GracePeriodRemaining"].ToString()) == 0)
            {
                MessageBox.Show("Windows is active");
                break;
            }
            else
            {
                MessageBox.Show(" Windows is not active");
                break;
            }
        }
            //now loop through the collection looking for
            //an activation status

        }

2 个答案:

答案 0 :(得分:2)

当您使用SoftwareLicensingProduct WMI类时,由于Windows Vista中引入了批量许可功能,因此返回的实例不止一个,因此要仅返回Windows版本的实例,您必须使用以下内容过滤WQL语句PartialProductKeyApplicationIdLicenseIsAddon属性。

试试这个样本

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                string ComputerName = "localhost";
                ManagementScope Scope;                
                Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);

                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT * FROM SoftwareLicensingProduct Where PartialProductKey <> null AND ApplicationId='55c92734-d682-4d71-983e-d6ec3f16059f' AND LicenseIsAddon=False");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    Console.WriteLine("{0,-35} {1,-40}", "Name", (String)WmiObject["Name"]);
                    Console.WriteLine("{0,-35} {1,-40}", "GracePeriodRemaining", (UInt32) WmiObject["GracePeriodRemaining"]);// Uint32
                    switch ((UInt32)WmiObject["LicenseStatus"])
                    {
                        case 0: Console.WriteLine("{0,-35} {1,-40}", "LicenseStatus", "Unlicensed");
                                break;
                        case 1: Console.WriteLine("{0,-35} {1,-40}", "LicenseStatus", "Licensed");
                                break;
                        case 2: Console.WriteLine("{0,-35} {1,-40}", "LicenseStatus", "Out-Of-Box Grace Period");
                                break;
                        case 3: Console.WriteLine("{0,-35} {1,-40}", "LicenseStatus", "Out-Of-Tolerance Grace Period");
                                break;
                        case 4: Console.WriteLine("{0,-35} {1,-40}", "LicenseStatus", "on-Genuine Grace Period");
                                break;
                        case 5: Console.WriteLine("{0,-35} {1,-40}", "LicenseStatus", "Notification");
                                break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}

另一种选择是使用SLIsGenuineLocal功能。

尝试使用此问题Determine Genuine Windows Installation in C#的答案获取C#样本。

答案 1 :(得分:0)

Windows®7,VOLUME_KMSCLIENT频道是指实际产品。这可以在description属性

中找到
    string ComputerName = "localhost";
    ManagementScope Scope;                
    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);

     Scope.Connect();
     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())
        {

            foreach (ManagementObject m in obj)
            {
                String s = m["Description"].ToString();
                if ((m["Description"].ToString()) .Contains("VOLUME_KMSCLIENT channel"))
                {
                    if (m["LicenseStatus"].ToString() == "1")
                    {
                        var gracePeriod = Convert.ToInt32(m["GracePeriodRemaining"]) / (60 * 24);
                        MessageBox.Show("Not Licensed " + " Grace Period Remaining--" + gracePeriod.ToString() + "days");

                    }
                    else
                    {
                        var gracePeriod = Convert.ToInt32(m["GracePeriodRemaining"]) / (60 * 24);
                        MessageBox.Show("Not Licensed " + " Grace Period Remaining--" + gracePeriod.ToString() + "days");
                    }
                }

            }
        }