C# how to refresh serial port in c#
<p>i want to make my application can refresh serial port in c#. when list of port(In ComboBox) is empty and i hit button <code>refresh</code>, it's work perfectly and show the list of active port. but if i disconnected the <code>Serial Port</code> and i hit <code>refresh</code> button, actually it's must make the list of port(In Combobox) is empty because the serial port is disconnected. So how to make when i hit <code>refresh</code> button and the condition is disconnected, we make all of port list(in Combobox ) is empty ?</p>

<p>this is my code in refresh button:</p>

<pre><code>private void button2_Click_2(object sender, EventArgs e)
{
    if(String.IsNullOrEmpty(cboPort.Text))
    {
        comm.SetPortNameValues(cboPort);

        for (int i = 0; i &lt; cboPort.Items.Count; i++)
        {
            string value = cboPort.GetItemText(cboPort.Items[i]);

            if (String.IsNullOrEmpty(value))
            {
                string a = cboPort.SelectedIndex.ToString();
                return;
            }
            else
            {
                cboPort.SelectedIndex = 0;
            }
        }
     }
     else if ((cboPort.Text) != " " &amp;&amp; cboPort.SelectedIndex == -1)
     {
           cboPort.Text = " ";
           return;
     }
}
</code></pre>

<p>this is my code in setportnamevalues :</p>

<pre><code>    public void SetPortNameValues(object obj)
    {
        foreach (string str in SerialPort.GetPortNames())
        {
             ((ComboBox)obj).Items.Add(str);   
        }
    }
</code></pre>

<p>my expetation is :</p>

<p><code>1. i connect serial port 
2. i run my app 
3. i disconnect serial port
4. i hit refresh
5. final result is port list empty in combobox</code></p>

<p>thanks for helping and responses, i am still new in c#. Greetings! </p>