gpt4 book ai didi

c# - WMI Code Creator for Bitlocker Status - 我哪里出错了?

转载 作者:太空宇宙 更新时间:2023-11-03 23:35:39 25 4
gpt4 key购买 nike

enter image description here我正在使用 WMI Code Creator,代码看起来可以从应用程序正常运行。但是,它在我的代码内部出现了我似乎无法摆脱的错误。我应该为这个工作提供引用吗?如果是这样,我在哪里可以得到它?

    public class MyWMIQuery
{
public static void Main()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2\\Security\\MicrosoftVolumeEncryption",
"SELECT * FROM Win32_EncryptableVolume");

foreach (System.Management.ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_EncryptableVolume instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("ProtectionStatus: {0}", queryObj["ProtectionStatus"]);
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
}
}

enter image description here
(点击查看大图)

最佳答案

如果您在 MSDN 中搜索 ManagementObjectSearcher,您会得到 this page .在 .NET 类的每个 MSDN 页面上,您将在页面顶部看到两条信息。

Namespace: System.Management
Assembly: System.Management (in System.Management.dll)

第一行告诉您需要添加using System.Management; 或执行System.Management.ManagementObjectSearcher 如果您想要引用该对象。

第二行告诉您您的项目必须引用文件 System.Management.dll 以便您的代码能够找到该类。如果您在 Add Reference Dialog 中搜索您需要在列表中查找 System.Management(括号外的部分)。

enter image description here


问题的第二部分是您的项目中有一个名为 ComplianceGuide.ManagementObject 的类,而 Visual Studio 正在选择该引用而不是 System.Management.ManagementObject , 用

替换你的 foreach
foreach (System.Management.ManagementObject queryObj in searcher.Get())

强制它使用正确的类。

关于c# - WMI Code Creator for Bitlocker Status - 我哪里出错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30673984/

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