Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Aam trying to populate list of available ports in my system.
Following is my code which works in windows xp, but does not work in windows 7

public void loadPorts()
        {
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT * FROM Win32_PnPEntity");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    if (queryObj["Caption"].ToString().Contains("(COM"))
                    {
                        cmbPort.Items.Add(queryObj["Caption"]);
                    }

                }
            }
            catch (ManagementException e)
            {
            }
        }
Posted
Comments
lukeer 28-May-13 5:58am    
What is the desired behaviour?
What does actually happen?
Is there an error message?
What line does yields the error?
Himu from Orissa 28-May-13 5:59am    
aam not getting the list of com ports in the combobox

1 solution

Try:
C#
string[] ports = SerialPort.GetPortNames();
foreach (string p in ports)
    {
    Console.WriteLine(p);
    }
 
Share this answer
 
Comments
Himu from Orissa 28-May-13 6:43am    
actually i need the name also e.g.(Communication Port), but in this method it only gives com1,com2 etc

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900