How to Start Windows Service As Administrator Privileges

拈花ヽ惹草 提交于 2019-12-24 17:01:44

问题


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


回答1:


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




回答2:


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>


来源:https://stackoverflow.com/questions/30818247/how-to-start-windows-service-as-administrator-privileges

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!