[英]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 Studio或WMI 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的答案:
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.