Share via


WMI not providing serial ports

Question

Saturday, May 23, 2015 8:45 PM

I am using the following code to search for the serial ports:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from WIN32_SerialPort");
            foreach(ManagementObject port in searcher.Get())
                Console.WriteLine(  (string)port.GetPropertyValue("Name") ); 

                         String[] portnames = SerialPort.GetPortNames();

I am using Windows 8.1 / 64bit.

The ManagementObjectSearcher query returns no objects. However, the SerialPort.GetPortNames() clearly identifies the COM ports that are attached.

How can I get the descriptive port names using WMI?

james e kain

All replies (4)

Tuesday, May 26, 2015 5:31 AM âś…Answered | 2 votes

My WMI code once worked just fine -- but it seemed to cease functioning after a Windows Update some time ago.

Which update is it?

If all Win32_SerialPort and MSSerial_PortName don't work, please try another object Win32_PnPEntity, this will show all Plug and Play entries in the Device Manager:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=\"{4d36e978-e325-11ce-bfc1-08002be10318}\"");
            foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("Name: {0}", queryObj["Name"]);
            }

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Monday, May 25, 2015 10:03 AM

Hi james,

SerialPort.GetPortNames method checks this registry key to get the port names, but if the registry contains stale or otherwise incorrect data then the GetPortNames method will return incorrect data, as the documentation says.

To make sure your WMI query is correct, I recommend you use WMICodeCreator to generate the code, for example, this generated code works fine to get the serialport names:

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher = 
                    new ManagementObjectSearcher("root\\CIMV2", 
                    "SELECT * FROM Win32_SerialPort"); 

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("");
                    Console.WriteLine("Win32_SerialPort instance");
                    Console.WriteLine("");
                    Console.WriteLine("Name: {0}", queryObj["Name"]);
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}

Or try other WMI objects, for example:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName");
            foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("InstanceName: {0}", queryObj["InstanceName"]);
                Console.WriteLine("PortName: {0}", queryObj["PortName"]);
            }

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Monday, May 25, 2015 3:29 PM

I added the code as you suggested (both methods) and the Query returned no results and there is no exception from the query statement.

However SerialPort.GetPortNames()  identifies a connected COM port as is also shown in the device manager.

I have also tried this adding a Manifest and launching the application with Administrator privilege.

My WMI code once worked just fine -- but it seemed to cease functioning after a Windows Update some time ago.

james e kain


Wednesday, June 3, 2015 3:25 AM

I temporarily close your question, if you got some questions, please unmark the reply and follow up here.

Thanks for your understanding.

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.