【问题标题】:How do I programmatically determine which WMI property is the primary key of a class?如何以编程方式确定哪个 WMI 属性是类的主键?
【发布时间】:2013-06-17 11:01:03
【问题描述】:

我需要动态确定 WMI 类的哪个属性是 C# 中的主键。

我可以使用CIM StudioWMI Delphi Code Creator 手动定位此信息,但我需要找到一个类的所有属性名称和标志,即 / 是键 / 键...我已经知道如何找到类的属性名称。

related answer 中介绍了密钥的手动识别,我希望作者(我正在查看 RRUZ)可以向我介绍他们如何找到密钥(或其他任何可能知道的人) )。

非常感谢。

【问题讨论】:

    标签: c# wmi


    【解决方案1】:

    要获取 WMI 类的关键字段,您必须遍历 WMI 类的属性的 qualifiers,然后搜索名为 key 的限定符,最后检查该限定符的值是否为 @987654324 @。

    试试这个示例

    using System;
    using System.Collections.Generic;
    using System.Management;
    using System.Text;
    
    namespace GetWMI_Info
    {
        class Program
        {
    
            static string GetKeyField(string WmiCLass)
            {
                string key = null; 
                ManagementClass manClass = new ManagementClass(WmiCLass);
                manClass.Options.UseAmendedQualifiers = true;
                foreach (PropertyData Property in manClass.Properties)
                    foreach (QualifierData Qualifier in Property.Qualifiers)
                        if (Qualifier.Name.Equals("key") && ((System.Boolean)Qualifier.Value))                        
                            return Property.Name;
                return key;                                                    
            }
    
            static void Main(string[] args)
            {
                try
                {
                    Console.WriteLine(String.Format("The Key field of the WMI class {0} is {1}", "Win32_DiskPartition", GetKeyField("Win32_DiskPartition")));
                    Console.WriteLine(String.Format("The Key field of the WMI class {0} is {1}", "Win32_Process", GetKeyField("Win32_Process")));
                }
                catch (Exception e)
                {
                    Console.WriteLine(String.Format("Exception {0} Trace {1}", e.Message, e.StackTrace));
                }
                Console.WriteLine("Press Enter to exit");
                Console.Read();
            }
        }
    }
    

    【讨论】:

    • 谢谢!我知道这很简单,但我找不到。
    【解决方案2】:

    对于那些感兴趣的人,我通过以下方式扩展了 RRUZ 的答案:

    • 允许针对远程计算机运行查询,并且
    • 添加对具有多个主键的类的支持(如 Win32_DeviceBus 的情况)。

      static void Main(string[] args)
      {
          foreach (var key in GetPrimaryKeys(@"root\cimv2\win32_devicebus"))
          {
              Console.WriteLine(key);
          }
      }
      
      static List<string> GetPrimaryKeys(string classPath, string computer = ".")
      {
          var keys = new List<string>();
          var scope = new ManagementScope(string.Format(@"\\{0}\{1}", computer, System.IO.Path.GetDirectoryName(classPath)));
          var path = new ManagementPath(System.IO.Path.GetFileName(classPath));
          var options = new ObjectGetOptions(null, TimeSpan.MaxValue, true);
          using (var mc = new ManagementClass(scope, path, options))
          {
              foreach (var property in mc.Properties)
              {
                  foreach (var qualifier in property.Qualifiers)
                  {
                      if (qualifier.Name.Equals("key") && ((System.Boolean)qualifier.Value))
                      {
                          keys.Add(property.Name);
                          break;
                      }
                  }
              }
          }
          return keys;
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-03
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2011-04-25
      相关资源
      最近更新 更多