添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

On Windows 10 Pro, version 20H2, using Visual Studio 2019 Version 16.9.6, Target Framework ".NET Framework 4.7 2".
RE: "How to: Query for Specific Instances of a Class (C#)" - ("https://learn.microsoft.com/en-us/previous-versions/windows/desktop/wmi_v2/how-to-query-for-specific-instances-of-a-class")

I added the following line to my code, and "Management" has a red squiggly under it, and error CS0234.
using Microsoft.Management.Infrastructure;

I can't find a "Microsoft.Management.Infrastructure" assembly to reference.

I added the following line to my code, and "CimSession" has a red squiggly under it.
CimSession cimSession;

I selected "Show potential fixes", selected "Install package 'Microsoft.Management.Infrastructure'", and selected "Find and install latest version. I got the error "Installing 'Microsoft.Management.Infrastructure' failed."

Then I again selected "Show potential fixes", selected "Install package 'Microsoft.Management.Infrastructure'", and selected "Install with package manager...". I browse and select 'Microsoft.Management.Infrastructure' (Version 2.0.0), and select 'Install'." I got "Install failed. Rolling back..."

I read somewhere that using classes in the System.Management namespace to access WMI was outdated, if not deprecated, and that new development should use Microsoft.Management.Infrastructure (MI) namespaces and their contained classes to use CIM.
My ultimate goal is to find an SD Card connected to the PC, and detect insertions and removals, from a C# WPF application.

How do I use Microsoft Management Infrastructure to access CIM or WMI data in a C# WPF app?

It seems that the package can be added in case of .NET 5 or .NET Core. Try experimenting with a new console project.

Maybe you should consider code that is based on System.Management assembly.

(from: https://learn.microsoft.com/en-us/windows/win32/wmisdk/using-wmi )
* "System.Management was the original namespace that covered managed code for WMI. However, the
* underlying technology for System.Management is generally slower than, and does not scale as
* well as, Microsoft.Management.Infrastructure. As such, it is not recommended that you use
* System.Management for new projects."

Not true! Microsoft Management Infrastructure can NOT be used in .NET Framework projects, regardless of whether 'old' or 'new'. You must use System.Management for new .NET Framework projects.

To answer the question:

How do I use Microsoft Management Infrastructure to access CIM or WMI data in a C# WPF app?

Use the System.Management Namespace . I do not know why they are saying Microsoft.Management.Infrastructure. Be sure to add both a using and a reference for System.Management. The following code will list the USB drives.

SelectQuery q = new SelectQuery("SELECT * FROM Win32_DiskDrive Where InterfaceType='USB'");  
ManagementObjectSearcher s = new ManagementObjectSearcher(q);  
StringBuilder sb = new StringBuilder();  
foreach (ManagementObject drive in s.Get())  
    sb.Append($"{drive["caption"]} | {drive["MediaType"]}\r\n");  
InfoBox.Text = sb.ToString();  

Also if you do not know about the WMI Explorer then get that; it is worth trying. It will take a little time to learn how to use but it can be very useful.

I can't find a "Microsoft.Management.Infrastructure" assembly to reference.

Please right click your project > Choose Manage Nuget Package... in popup window> Click Brower Tab and input Microsoft.Management.Infrastructure in search TextBox> click the Microsoft.Management.Infrastructure in result list and install it. Then you can add using Microsoft.Management.Infrastructure to your project.

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Thank you for your response. I think you may have misunderstood part of my question. I said:

I selected "Show potential fixes", selected "Install package 'Microsoft.Management.Infrastructure'", and selected "Find and install latest version. I got the error "Installing 'Microsoft.Management.Infrastructure' failed."

Then I again selected "Show potential fixes", selected "Install package 'Microsoft.Management.Infrastructure'", and selected "Install with package manager...". I browse and select 'Microsoft.Management.Infrastructure' (Version 2.0.0), and select 'Install'." I got "Install failed. Rolling back..."

Here is what my Visual Studio 'Output' window showed:

(Too long. "1000 characters exceeded" Please see attached "NuGet Install Output.txt".)