title: Pass an argument to a Windows Service at automatic startup
body: <p>I found some similar questions, yet the answers doesn't seem to help in my case. I wish to configure my service for automatic startup with 1 argument.</p>
  
  <p>My service OnStart method looks like this:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Starts the service
      /// &lt;/summary&gt;
      /// &lt;param name="args"&gt;args must contain the listening port number of the service&lt;/param&gt;
      protected override void OnStart(string[] args)
      {
          if (args != null &amp;&amp; args.Length &gt; 0)
          {
              int port = -1;
              if (int.TryParse(args[0], out port) &amp;&amp; port &gt;= 0 &amp;&amp; port &lt;= 65535)
              {
                  server.Start(port);
              }
              else
              {
                  Log.Entry("Port value " + args[0] + " is not a valid port number!", Log.Level.Error);
              }
          }
          else
          {
              Log.Entry("Service must be started with the port number (integer) as parameter.", Log.Level.Error);
              throw new ArgumentNullException("Service must be started with the port number (integer) as parameter."); // stop the service!
          }
      }
  </code></pre>
  
  <p>So I registered my service with the int parameter (8081) after the service file name like in the screenshot below (as suggested in other answers to similar questions).</p>
  
  <p><a href="https://i.stack.imgur.com/vBDgY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vBDgY.png" alt="enter image description here"></a></p>
  
  <p>When I start the service, I always get the error message "The service must be started....".</p>
  
  <p>If I type an integer in the "Start parameters:" field, the service starts normally.</p>
  
  <p>How can I have Windows automatically start my service with one argument (8081)?</p>
  
  <p><strong>Edit:</strong> </p>
  
  <blockquote>
    <p>I did some more tests. Added logging of the args[] parameter. It is
    empty. Also I tried to add extra parameters like in this image:</p>
    
    <p><a href="https://i.stack.imgur.com/4erK9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4erK9.png" alt="enter image description here"></a></p>
    
    <p>I tried both with and without double-quotes around the arguments, but
    they are not passed to the service.</p>
  </blockquote>