How to Start Windows Service As Administrator Privileges

18.2k Views Asked by At

I have my own application server which is windows service who communicates with the sql server, in some cases sql server service is stop so I am stating that via this code

ServiceController sc = new ServiceController("MSSQL$SQLEXPRESS");
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);

but it requires administrator privileges to start service how can I start my window service as administrator

2

There are 2 best solutions below

1
On BEST ANSWER

i just add this tag in my app.manifest file <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> it works ...

0
On

Lets split everything into steps

  1. Add file "Application Manifest File" to you root catalog
  2. Select project properties
  3. In application tab Recources group find Manifest field
  4. Paste you new manifest file name. Probably "app.manifest"
  5. Fill file with this info

All information could be a little bit different based on you situation. More info

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app" />
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
      <applicationRequestMinimum>
        <defaultAssemblyRequest permissionSetReference="Custom" />
        <PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
      </applicationRequestMinimum>
    </security>
  </trustInfo>
</asmv1:assembly>