简体   繁体   English

如何以编程方式确定哪个WMI属性是类的主键?

[英]How do I programmatically determine which WMI property is the primary key of a class?

I need to dynamically determine which property of a WMI class is the primary key in C#. 我需要动态确定WMI类的哪个属性是C#中的主键。

I can manually locate this information using CIM Studio or the WMI Delphi Code Creator but I need to find all property names of a class and flag which is / are the key / keys... and I already know how to find the property names of a class. 我可以使用CIM StudioWMI Delphi Code Creator手动查找此信息,但我需要查找类的所有属性名称和标记哪些是键/键...我已经知道如何查找属性名称一类。

Manual identification of the key is covered in a related answer and I'm hoping the author (I'm looking at RRUZ) might be able to fill me in on how they locate the key (or anyone else who might know). 手动识别密钥在相关的答案中有所涉及,我希望作者(我正在看RRUZ)可能能够让我了解他们如何找到密钥(或任何可能知道的人)。

Many thanks. 非常感谢。

To get the key field of a WMI class, you must iterate over the qualifiers of the properties for the WMI class and then search for the qualifier called key and finally check if the value of that qualifier is true . 要获取WMI类的键字段,必须迭代WMI类的属性的qualifiers ,然后搜索名为key的限定符,最后检查该限定符的值是否为true

Try this sample 试试这个样本

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

For those interested I've expanded on RRUZ's answer by: 对于那些感兴趣的人,我已经通过以下方式扩展了RRUZ的答案:

  • allowing the query to be run against a remote machine, and 允许对远程计算机运行查询,以及
  • adding support for classes with multiple primary keys (as is the case with Win32_DeviceBus ). 添加对具有多个主键的类的支持(与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; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建类的字典,以便我可以使用键来确定要初始化的新类? - How do I create a Dictionary of classes, so that I can use a key to determine which new class I want to initialize? 如何为继承IdentityUser的类创建主键 - How do I create primary key for class that inherits IdentityUser 如何使用WMI确定连接哪些打印机 - How to Determine Which Printers are Connected using WMI 我如何确定接口包含的类 - how do i determine which class an interface contains 如何将变量严格键入为特定的 WMI 类? - How do I strictly-type a variable as a specific WMI class? 我有一个表,该表具有由三列组成的主键。 如何索引此表? - I have a table which has a primary key that is made of three columns. How do I index this table? 如何生成非随机的主键? - How do I generate a primary key that is not random? 如何确定属性是否被覆盖? - How do I determine if a property was overridden? 如何以编程方式检查安装了哪个版本的 WMI - How can programmatically check which version of the WMI is installed 如何以编程方式复制具有复合键的MS Access表架构? - How do I programmatically copy MS Access table schema which has composite key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM