如何以管理员权限启动Windows服务

4
我有自己的应用服务器,它是一个与SQL Server通信的Windows服务。在某些情况下,SQL Server服务会停止,因此我通过以下代码来说明:
ServiceController sc = new ServiceController("MSSQL$SQLEXPRESS");
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);

但是它需要管理员权限才能启动服务,如何以管理员身份启动我的Windows服务?


你尝试以管理员身份运行启动服务的应用程序了吗? - Alioza
但是这两个应用程序都是Windows服务。我该如何将我的Windows服务以管理员身份运行? - Jenish Zinzuvadiya
我只是在我的 app.manifest 文件中添加了这个标签 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />,它就可以工作了... - Jenish Zinzuvadiya
然后将其作为答案写下来,这样这个问题就不会被视为未回答的了。 - Orkun Bekar
2个回答

6

让我们把所有内容分成几个步骤:

  1. 将“应用程序清单文件”添加到您的根目录中
  2. 选择项目属性
  3. 在“应用程序”选项卡下的“资源”组中找到“清单”字段
  4. 粘贴您的新清单文件名称,可能为“app.manifest”
  5. 使用以下信息填写该文件

所有信息可能因您的情况而略有不同。 更多信息

<?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>

6

我刚刚在我的app.manifest文件中添加了这个标签: <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> 它起作用了...


VS2022可以生成基础文件。注释会告诉你如何配置: https://dev59.com/8XMOtIcB2Jgan1znc_vP - HackSlash

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接