title: Port Selection and Batch Runner Application
body: <p>I have never written an application, so I will post the entirety of the code (80 lines). (I come from a background of putting together scripts.)</p>
  
  <p>My goal is to load, or create a list of "used" ports, choose a number within a range that isn't on the list, and if the amount of tries to reach an unused port reaches 129, to run a windows batch file.</p>
  
  <p>This also would turn the chosen port into a .cmd</p>
  
  <p>(some of this is an amalgamation of sources from SO)</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Linq;
  using System.Diagnostics;
  using System.IO;
  namespace randomport
  {
      class Core
      {
          public const int 
              minval = 8001,
              maxval = 8128;
          public static int[] usedPorts = new int[]{};
          public static int 
              chosenPort = 0,
              tries = 0,
              timeout = 10;
          public static bool read = false;
          public static void Main()
          {
              if(!read)
              {
                  Read();
                  read = true;
              }
              RandGen();
          }
          public static void RandGen()
          {
              Process proc = null;
              Random rand = new Random();
              if(tries &lt; 129) chosenPort = rand.Next(minval, maxval);
              else
              {
                  proc.StartInfo.FileName                 = @"C:\Users\Administrator\Desktop\TerrariaServer\filebin\sendservfull.bat";
                  proc.StartInfo.RedirectStandardError    = true;
                  proc.StartInfo.RedirectStandardOutput   = true;
                  proc.StartInfo.UseShellExecute          = false;
  
                  proc.Start();
  
                  proc.WaitForExit
                      (
                          (timeout &lt;= 0)
                          ? int.MaxValue : timeout * 100 * 60
                      );
              }
              for(int i = 0; i &lt; usedPorts.Length; i++)
              {
                  if(chosenPort != usedPorts[i])
                  {
                      Write();
                  //  Application.Exit();
                  }
                  else
                  {
                      tries += 1;
                      return;
                  }
              }
          }
          public static void Read()
          {
              using (StreamReader sr = new StreamReader(@"C:\Users\Administrator\Desktop\TerrariaServer\filebin\activeports.txt")) 
              {
                  string[] values = sr.ReadToEnd().Split(';');
                  for(int i = 0; i &lt; values.Length; i++)
                  {
                      int.TryParse(values[i], out usedPorts[i]);
                  }
              }
          }
          public static void Write()
          {
              File.AppendAllText(@"C:\Users\Administrator\Desktop\TerrariaServer\filebin\activeports.txt", "set port="+chosenPort+";");
              File.Move(@"C:\Users\Administrator\Desktop\TerrariaServer\filebin\activeports.txt", Path.ChangeExtension(@"C:\Users\Administrator\Desktop\TerrariaServer\filebin\activeports.txt", ".cmd"));
          }
      }
  }
  </code></pre>
  
  <p>I have a little work to do on the final export (like removing ";").</p>
  
  <p>The script compiles, but does not run as intended. Something is definitely wrong, but I am unaware of it. If it is something obvious, I guess that would be handy, otherwise if it is simply format and so on, I clearly need to do a little more studying.</p>