title: Automatically adjust COM serial port in C#
body: <p>I've got Arduino Nano controlling a DC motor connected to my PC and controlled over C#.
  The only problem is that it works on my computer at the moment, and if I connect it to another PC it won't work unless it uses the same serial port. That's why I want the COM port to "set itself". Is it possible to do? If not, I wanted to make another Form just for entering the number of COM port, but I want to avoid that if possible. Thank you in advance.
  This is my code:</p>
  
  <pre><code>public partial class Form1 : Form
  {
      String s = "0";
      string brojPorta = "COM5";
      int vrijednost = 0;
      System.IO.Ports.SerialPort serialPort1;
      public Form1()
      {
          InitializeComponent();
          System.ComponentModel.IContainer components =
      new System.ComponentModel.Container();
          serialPort1 = new System.IO.Ports.SerialPort(components);
          serialPort1.PortName = brojPorta;
          serialPort1.BaudRate = 9600;
          serialPort1.Open();
      }
      private void Form1_FormClosing(object sender, FormClosingEventArgs e)
      {
          if (serialPort1.IsOpen) serialPort1.Close();
      }
  
      private void Klizac1_Scroll(object sender, ScrollEventArgs e)
      {
          vrijednost = Klizac1.Value;
          s = (vrijednost * 10.24).ToString();
          serialPort1.Write(s + '\n');
          label1.Text = ((vrijednost-50)*2).ToString()+"%";
      }
  
      private void btn_Zaustavi_Click(object sender, EventArgs e)
      {
          Klizac1.Value = 50;
          label1.Text = "0";
          s = (Klizac1.Value * 10.24).ToString();
          serialPort1.Write(s + '\n');
      }
  }
  </code></pre>