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

Jeff Nygren 121 Reputation points
2021-06-17T21:49:47.467+00:00

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?

Developer technologies | Windows Presentation Foundation
{count} votes

Accepted answer
  1. Sam of Simple Samples 5,546 Reputation points
    2021-06-18T21:02:10.313+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. DaisyTian-1203 11,646 Reputation points
    2021-06-18T02:14:39.36+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.