title: (Re)Starting a Windows Service from out of my C# code does not work
body: <p>To update my self-written <strong>Windows Service</strong>, I wrote a C# program that stops the service, copies the new DLLs and then restarts it.</p>
  
  <p>My first approach was the following: </p>
  
  <pre class="lang-cs prettyprint-override"><code>ServiceController service = new ServiceController(serviceName);
  service.Stop();
  service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
  // (...) copy the dll files
  service.Start();
  service.WaitForStatus(ServiceControllerStatus.Running, timeout);   
  </code></pre>
  
  <p>While stopping the service worked, the start method produced the following error:</p>
  
  <pre class="lang-none prettyprint-override"><code>Cannot start service FOVEA Service Debug on computer '.'.   
  at System.ServiceProcess.ServiceController.Start(String[] args)    at
  System.ServiceProcess.ServiceController.Start()
  </code></pre>
  
  <p>Running my update program as Administrator made no difference.</p>
  
  <p>So I tried to start/stop the service in the command line via <code>net start/stop service</code> which worked perfectly and I changed my C# program to the following:</p>
  
  <pre class="lang-cs prettyprint-override"><code>Process p = Process.Start("net", "stop \"" + serviceName + "\"");
  p.WaitForExit();
  p = Process.Start("net", "start \"" + serviceName + "\"");
  p.WaitForExit(); 
  </code></pre>
  
  <p>Like before, stopping the service worked. The call of <code>net start service</code> however ouputted </p>
  
  <pre class="lang-none prettyprint-override"><code>The service is not responding to the control function
  more help is available by typing NET HELPMSG 2186
  </code></pre>
  
  <p>The service is installed for the local logged in user (Administrator) but I don't see why <code>net start</code> works from the command line but not out of the code. I also tried the following:</p>
  
  <ul>
  <li>Set up full permissions for my update program for user <code>NETWORK SERVICE</code> </li>
  <li>Started the process with <code>p.StartInfo.Verb = "runas"</code></li>
  <li>Changed the <code>p.StartInfo.WorkingDirectory</code> to <code>Environment.SystemDirectory</code></li>
  <li>Changed the <code>p.MachineName</code> to <code>Environment.MachineName</code></li>
  <li>Let the thread sleep for 5 seconds after stopping the service</li>
  </ul>
  
  <p>But nothing seemed to make any impact.</p>
  
  <p>Any ideas how to get what I want? Thanks in advance for your help.</p>