Reputation: 531
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:\"");
Upvotes: 53
Views: 111369
Reputation: 61
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.
Upvotes: 1
Reputation: 321
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.
Upvotes: 1
Reputation:
~ just add System.management using nuget manager, It worked for me! c#
Upvotes: 5
Reputation: 281
In Solution Explorer, right click on References, then Add Reference ... and under Framework, you should activate the System.Management framework.
Upvotes: 28
Reputation: 21
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());
}
Upvotes: 2
Reputation: 1281
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.
Upvotes: 128
Reputation: 13296
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.
Upvotes: 12
Reputation: 6955
Have you added a reference to the System.Management assembly?
Upvotes: 28
Reputation: 351526
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.
Upvotes: 0