title: UAC or Userlevel how to overcome it!
body: <p>I'm trying to programmatically restart a service from within a helper application written in C# (on .net 4.0) but I get a permission violation if I run the EXE by double clicking while right clicking and doing "run as Administrator" works.</p>
  
  <p>But why do I need this the users are local admins?!</p>
  
  <p>I would like the app to run normally and only request admin rights when the user click the button to restart the service. Can this be done?</p>
  
  <p>The solution needs to work on xp, vista and windows 7.</p>
  
  <p>I'm using this code from <a href="http://www.csharp-examples.net/restart-windows-service/" rel="nofollow">http://www.csharp-examples.net/restart-windows-service/</a></p>
  
  <pre><code>public static void RestartService(string serviceName, int timeoutMilliseconds)
  {
    ServiceController service = new ServiceController(serviceName);
    try
    {
      int millisec1 = Environment.TickCount;
      TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
  
      service.Stop();
      service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
  
      // count the rest of the timeout
      int millisec2 = Environment.TickCount;
      timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1));
  
      service.Start();
      service.WaitForStatus(ServiceControllerStatus.Running, timeout);
    }
    catch
    {
      // ...
    }
  }
  </code></pre>