开发者

ManagementObject Class not showing up in System.Management namespace

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


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.


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


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


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.


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


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


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜