2

我正在使用下面的代码来确定 Windows7 的激活。我得到 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

        }
4

2 回答 2

2

当您使用SoftwareLicensingProductWMI 类时,由于 Windows Vista 中引入的批量许可功能,会返回多个实例,因此要仅返回您的 Windows 版本的实例,您必须使用PartialProductKey, ApplicationIdLicenseIsAddon属性过滤 WQL 语句。

试试这个样本

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# 示例。

于 2012-10-18T00:58:18.060 回答
0

Windows® 7,VOLUME_KMSCLIENT 频道指的是实际产品。这可以在描述属性中找到

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

            }
        }
于 2012-10-25T01:05:30.637 回答