【问题标题】:Issues in Finding Windows Activation through SoftwareProductLicensing Class通过软件产品许可类查找 Windows 激活的问题
【发布时间】:2012-10-17 23:21:58
【问题描述】:

我正在使用下面的代码来确定 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

        }

【问题讨论】:

    标签: c#-4.0 wmi wmi-query


    【解决方案1】:

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

    试试这个示例

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

    【讨论】:

    • 我的问题是有 7 个条目......但即使是一个没有获得许可的条目,当我在本地 PC 上测试它时也证明它不是许可的。但是我的本地 PC 根据计算机信息获得许可。我正在尝试找出 7 个实例中的哪一个是操作系统关于许可证信息的正确参考
    • 你试过我回答的代码了吗?因为此代码过滤 WMI 实例以仅检索本地 Windows 实例。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-07-07
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多