locked
WMI and Virtual Ports

    Question

  • Hi Folks

    I am using C# 1.1 to find the Serial Ports in Machine. I am using the WMI classes with query as "select * from Win32_SerialPort".

    The problem with this approach is i get only non-virtual ports. Is there any way of find virtual ports using WMI ??

    Please do guide me ?

    Thanks n Regards
    Anu Viswan

    Anu Viswan : www.AnuViswan.blogspot.com
    Monday, March 16, 2009 6:41 AM

Answers

  • Hi Anu,

    Please consider using WMICodeCreator, a useful WMI tool, to get USB Serial/COM port adaptors.
    Code Snippet:  (add the reference to System.Managment)
            //using System.Management;   
            private void button1_Click(object sender, EventArgs e)  
            {  
                try 
                {  
                    ManagementObjectSearcher MOSearcher = new ManagementObjectSearcher("root\\WMI""SELECT * FROM MSSerial_PortName");  
     
                    foreach (ManagementObject MOject in MOSearcher.Get())  
                    {  
                        listBox1.Items.Add(MOject["InstanceName"]);//MSSerial_PortName instance  
                        listBox1.Items.Add(MOject["PortName"]);//MSSerial_PortName instance  
                          
                        //If the serial port's instance name contains USB it must be a USB to serial device  
                        if (MOject["InstanceName"].ToString().Contains("USB"))  
                        {  
                            listBox1.Items.Add(MOject["PortName"] + " is a USB to Serial Adapter/Converter");  
                        }  
                    }  
                }  
                catch (ManagementException me)  
                {  
                    MessageBox.Show("An error occurred while querying for WMI data: " + me.Message);  
                }  
            } 

    Best regards,
    Guo
    • Marked as answer by Guo Surfer Monday, March 23, 2009 2:17 AM
    Thursday, March 19, 2009 8:45 AM

All replies

  • Hi Anu,

    Please consider using WMICodeCreator, a useful WMI tool, to get USB Serial/COM port adaptors.
    Code Snippet:  (add the reference to System.Managment)
            //using System.Management;   
            private void button1_Click(object sender, EventArgs e)  
            {  
                try 
                {  
                    ManagementObjectSearcher MOSearcher = new ManagementObjectSearcher("root\\WMI""SELECT * FROM MSSerial_PortName");  
     
                    foreach (ManagementObject MOject in MOSearcher.Get())  
                    {  
                        listBox1.Items.Add(MOject["InstanceName"]);//MSSerial_PortName instance  
                        listBox1.Items.Add(MOject["PortName"]);//MSSerial_PortName instance  
                          
                        //If the serial port's instance name contains USB it must be a USB to serial device  
                        if (MOject["InstanceName"].ToString().Contains("USB"))  
                        {  
                            listBox1.Items.Add(MOject["PortName"] + " is a USB to Serial Adapter/Converter");  
                        }  
                    }  
                }  
                catch (ManagementException me)  
                {  
                    MessageBox.Show("An error occurred while querying for WMI data: " + me.Message);  
                }  
            } 

    Best regards,
    Guo
    • Marked as answer by Guo Surfer Monday, March 23, 2009 2:17 AM
    Thursday, March 19, 2009 8:45 AM
  • Hi Anu,

    Please consider using WMICodeCreator, a useful WMI tool, to get USB Serial/COM port adaptors.
    Code Snippet:  (add the reference to System.Managment)
            //using System.Management;   
            private void button1_Click(object sender, EventArgs e)  
            {  
                try 
                {  
                    ManagementObjectSearcher MOSearcher = new ManagementObjectSearcher("root\\WMI""SELECT * FROM MSSerial_PortName");  
     
                    foreach (ManagementObject MOject in MOSearcher.Get())  
                    {  
                        listBox1.Items.Add(MOject["InstanceName"]);//MSSerial_PortName instance  
                        listBox1.Items.Add(MOject["PortName"]);//MSSerial_PortName instance  
                          
                        //If the serial port's instance name contains USB it must be a USB to serial device  
                        if (MOject["InstanceName"].ToString().Contains("USB"))  
                        {  
                            listBox1.Items.Add(MOject["PortName"] + " is a USB to Serial Adapter/Converter");  
                        }  
                    }  
                }  
                catch (ManagementException me)  
                {  
                    MessageBox.Show("An error occurred while querying for WMI data: " + me.Message);  
                }  
            } 

    Best regards,
    Guo

    Great! This works on XP but on Windows 7 it generates an access denied error. What can I do to circumvent that (other than running as administrator or switchin my app to XP compatibility)? I'm looking into the impersonate syntax.
    Tuesday, December 22, 2009 7:21 PM