gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:32:33 25 4
gpt4 key购买 nike

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

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

key 的手动识别包含在 related answer 中我希望作者(我在看 RRUZ)可以告诉我他们是如何找到 key 的(或者其他任何可能知道的人)。

非常感谢。

最佳答案

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

试试这个例子

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

关于c# - 如何以编程方式确定哪个 WMI 属性是类的主键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17145926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com