Adeian
Adeian

Reputation: 13

WMI ManagementObjectSearcher Invalid Class

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

Upvotes: 1

Views: 3911

Answers (1)

Helen
Helen

Reputation: 97677

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

Upvotes: 1

Related Questions