52

I'm trying to write some WMI in my windows form and the ManagementObject is givin me the

"The type or namespace name 'ManagementObject' could not be found" Error

Here is my un-complete code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Security.Policy;
using System.Management;
using System.Management.Instrumentation;


namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {


            ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"C:\"");
Drew
  • 521
  • 1
  • 4
  • 4

9 Answers9

128

Right-click References on the right and manually add System.Management. Even though I included it in the using statement I still had to do this. Once I did, all worked fine.

anon
  • 1,281
  • 1
  • 8
  • 2
  • 14
    Why doesn't Visual Studio include the reference automatically when we directly tell it `using System.Management;`? :( – Filip Vondrášek Sep 27 '13 at 07:36
  • This is the correct behavior. The weird thing is that it recognizes `System.Management` even when its reference is not added! – Javid Nov 25 '16 at 06:28
  • Is this a bug or it has to be that way? I thought it is because I am using .net4.0 – Hrvoje T Nov 30 '18 at 06:57
  • Do note that if you have the csproj set to read only (for instance with Perforce), the reference list in Visual Studio will say it's in there but it won't actually be added! This error still shows up after doing this. – Anonymous Mar 29 '19 at 08:44
  • To anyone else that couldn't find it, once in the "References Manager", expand "Assemblies" and go to "Framework". Although for me, it was already ticked but still doesn't work on any machine that isn't the development machine. – user2924019 Jul 01 '20 at 20:15
28

Have you added a reference to the System.Management assembly?

Paul Nearney
  • 6,965
  • 2
  • 30
  • 37
26

In Solution Explorer, right click on References, then Add Reference ... and under Framework, you should activate the System.Management framework.

CFJH
  • 261
  • 3
  • 2
12

You need to add a reference to System.Management.dll to your project.

You can see System.Management.Instrumentation without adding a reference to System.Management.dll because it is included in a different library (System.Core.dll, which is included as a reference automatically), but you cannot access the other types contained by that namespace without explicitly adding a reference to the System.Management.dll library.

Murhaf Sousli
  • 12,622
  • 20
  • 119
  • 185
5

~ just add System.management using nuget manager, It worked for me! c#

2

I think the problem is there is no WMI object for Win32_LogicalDisk.DeviceID=\"C:\". Try to replace:

ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"C:\"");

with:

ManagementObject disk = new ManagementObject("Win32_LogicalDisk");

and then to step through each field:

foreach (ManagementObject o in disk.Get()){
    //Do what ever you need here.... For example:  
    Console.WriteLine(o.ToString());
}
hichris123
  • 10,145
  • 15
  • 56
  • 70
Tommy Lu
  • 21
  • 2
1

The version of Visual Studio that I have does not import ManagementObjectSearcher by importing "System.Management" namespace. If you have the same issue, try adding a reference to "System.Management.dll' by doing the following steps.

  1. Click on project properties on solution explorer in Visual Studio. Go to "References".
  2. Click on "Add" to add a new reference. Click on "Browse...".
  3. Navigate to "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727".
  4. Add a reference to "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Management.dll".
Meisam Rasouli
  • 301
  • 2
  • 5
1

This is quite an old post but I just had to troubleshoot this. The only way I got it working with Visual Basic 2022 was to download and install through the NuGet Installer. Manually adding the .dll did not work for me. Once NuGet Manager is open Search: System.Management and download the latest from Microsoft, hope this helps someone.

Tim Flinn
  • 61
  • 1
  • 6
0

Make sure your project isn't set up to compile against the .NET 4 Framework Client Profile.

Please see Namespace not recognized (even though it is there) for more details.

Community
  • 1
  • 1
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635