Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,
I tried to ask the question but was at the end of a Caffine fueled programming marathon, possibly not making sense, raging at the customer etc. The issue I have is I need to use a method like
C#
cboPorts.DataSource = SerialPort.GetPortNames;

How ever the app errors if it is opened on a PC with no Com Ports, it was hard to find this error as nearly every PC has a Serial Port and it was only possible to isolate it with a PC that only had USB com ports and these were removed before run it seems I need to check for any Serial Ports before I do any thing else.
I was doing the following after I had found the serial Ports
C#
do
{
  index += 1;
  cboPort.Items.Add(nameArray[index]);
    }
while (!((nameArray[index]==myComPortName)||(index == nameArray.GetUpperBound(0))));

I have have just moved this so it is the first thing in the Form Load I am now going to test it!

Is this the right way is there a right way?

Glenn
Posted

1 solution

Why wouldn't you just do this:
string[] portNames = SerialPort.GetPortNames();
if(portNames.Length == 0)
    // No ports found.
else
    // Do your binding.
 
Share this answer
 
Comments
glennPattonWork3 25-Oct-12 3:44am    
Umm, thanks for that I will give it a go, I have done something similar already.

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