locked
Unable to use System.Management RRS feed

  • Question

  • I am trying to create a console application to bring up the volume label of drive c:. However I'm having no end of problems trying to use System.Management, in particular ManagementObjectSearch.

    I have added System Management.dll to the References, and my code is as follows;

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Management;  
    namespace ConsoleApplication1  
     
    {  
     class Program  
     {  
      static void Main(string[] args)  
      {  
       ManagementObjectSearch search;  
      }  
     }  
    }  
     
     

    The error I am getting is The type or namespace name 'ManagementObjectSearch' could not be found (are you missing a using directive or an assembly reference?)

    Any ideas as to what I'm missing here?

    Wednesday, November 5, 2008 1:56 PM

Answers

  • Hi - I think my information can solve your problem!

    If you have added the "using System.Management;" - like this then you will not beable to access the "System Management class".

    How can I do then?

    When you made a new project, go to the Solution Explorer and right-click on references and in the list search for "System.Management" select it and press add.

    Then you will be enable to add the "System.Management;" and access the,

    ManagementObjectSearcher search = new ManagementObjectSearcher("SELECT * FROM 'any custom database from WMI');

    I hope this information can solve your problem.

    Have a nice day...

    Regards,
    Fisnik
    Coder24.com
    • Marked as answer by Harry Zhu Monday, November 10, 2008 3:36 AM
    Friday, November 7, 2008 6:30 PM
  • The class name is ManagementObjectSearcher, not ManagementObjectSearch. And it's not a static class, you'll have to create an instance:

    ManagementObjectSearcher searcher = new ManagementObjectSearcher();

    There are 7 constructors though, so you may not want the default one.

    Please mark the post if it answers your question.
    Wednesday, November 5, 2008 3:08 PM
  • Yep, it was indeed a typo. That'll teach me not to cut and paste code. Thanks for that.

    As it turns out I used a different method to get the volume label, namely DriveInfo from the System.IO resource;
            static void Main(string[] args)  
            {  
                int i=0;  
                DriveInfo[] allDrives = DriveInfo.GetDrives();  
                foreach (DriveInfo checkDrive in allDrives)  
                {  
                    try  
                    {  
                        if (allDrives[i].IsReady == true)  
                            if (allDrives[i].Name == "C:\\")  
                            {  
                                if (allDrives[i].VolumeLabel != "")  
                                    Console.WriteLine(allDrives[i].VolumeLabel);  
                            }  
                        ++i;  
                    }  
                    catch  
                    {  
                    }  
                }  
            } 
    Works quite well and solves my problems nicely.
    • Marked as answer by Harry Zhu Monday, November 10, 2008 3:36 AM
    Friday, November 7, 2008 3:49 PM

All replies

  • The class name is ManagementObjectSearcher, not ManagementObjectSearch. And it's not a static class, you'll have to create an instance:

    ManagementObjectSearcher searcher = new ManagementObjectSearcher();

    There are 7 constructors though, so you may not want the default one.

    Please mark the post if it answers your question.
    Wednesday, November 5, 2008 3:08 PM
  • Yep, it was indeed a typo. That'll teach me not to cut and paste code. Thanks for that.

    As it turns out I used a different method to get the volume label, namely DriveInfo from the System.IO resource;
            static void Main(string[] args)  
            {  
                int i=0;  
                DriveInfo[] allDrives = DriveInfo.GetDrives();  
                foreach (DriveInfo checkDrive in allDrives)  
                {  
                    try  
                    {  
                        if (allDrives[i].IsReady == true)  
                            if (allDrives[i].Name == "C:\\")  
                            {  
                                if (allDrives[i].VolumeLabel != "")  
                                    Console.WriteLine(allDrives[i].VolumeLabel);  
                            }  
                        ++i;  
                    }  
                    catch  
                    {  
                    }  
                }  
            } 
    Works quite well and solves my problems nicely.
    • Marked as answer by Harry Zhu Monday, November 10, 2008 3:36 AM
    Friday, November 7, 2008 3:49 PM
  • Hi - I think my information can solve your problem!

    If you have added the "using System.Management;" - like this then you will not beable to access the "System Management class".

    How can I do then?

    When you made a new project, go to the Solution Explorer and right-click on references and in the list search for "System.Management" select it and press add.

    Then you will be enable to add the "System.Management;" and access the,

    ManagementObjectSearcher search = new ManagementObjectSearcher("SELECT * FROM 'any custom database from WMI');

    I hope this information can solve your problem.

    Have a nice day...

    Regards,
    Fisnik
    Coder24.com
    • Marked as answer by Harry Zhu Monday, November 10, 2008 3:36 AM
    Friday, November 7, 2008 6:30 PM
  • Thankyou,

    This has solved the issue for me too.

    i had the problem using System.Management.ObjectQuery could not be found in Namespace System.Management

    Tested using window7 visualStudio2010 knowing that this thread is quite old this may help some others if they come across this thread.

    Sunday, October 17, 2010 7:55 PM