tags:

views:

200

answers:

1

Hello I've got a bit of a problem. I'm trying to use WMI to list information about disks. When I run the code from the WMI code creator everything returns fine and I get the information I'm looking for. When I run the code from the application I'm writing I get an invalid class error that gets thrown from the foreach loop.

The code I wrote and WMI generated is essentially the same, only the output is different. What could I possibly be doing wrong. Here is the code I wrote.

        public List<diskData> getDiskInfo()
    {
        List<diskData> dData = new List<diskData>();
        diskData mydisk = null;
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM W32_LogicalDisk");
            foreach (ManagementObject item in searcher.Get())
            {
                mydisk.name = Convert.ToString(item["Name"]);
            }
            return dData;

        }
        catch (Exception ex)
        {
            Console.WriteLine("This is the Message: " + ex.Message);
            return dData;
        }

    }

Thanks for any help you guys can provide.

Paul

+1  A: 

The Win32_LogicalDisk class name in your WMI query is misspelled as W32_LogicalDisk.

Helen
I hate it when I do that. Even worse when I do it so in public. :) Thanks for seeing it for me. I'm going to go hide under the covers for the rest of the day.
Adeian