49
votes

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:\"");
8

8 Answers

128
votes

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.

28
votes

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

24
votes

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

12
votes

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.

4
votes

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

2
votes

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

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.

0
votes

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".