简体   繁体   English

如何以管理员身份启动Windows Service

[英]How to Start Windows Service As Administrator Privileges

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 我有我自己的应用程序服务器,它是与sql server通信的Windows服务,在某些情况下sql server service已停止,因此我通过此代码进行说明

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 但是需要管理员特权才能启动服务如何以管理员身份启动窗口服务

我只是将此标签添加到我的app.manifest文件<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />就可以了...

Lets split everything into steps 让一切分成几步

  1. Add file "Application Manifest File" to you root catalog 将文件“ Application Manifest File”添加到您的根目录
  2. Select project properties 选择项目属性
  3. In application tab Recources group find Manifest field 在应用程序选项卡资源组中找到清单字段
  4. Paste you new manifest file name. 粘贴您新的清单文件名称。 Probably "app.manifest" 可能是“ 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM