2015-06-13 232 views
1

我是窗口服务谁与SQL服务器进行通信,在某些情况下,SQL Server服务停止,所以我声明我自己的应用服务器,它通过这个代码如何启动Windows服务作为管理员权限

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

但它需要管理员权限启动服务如何开始我的窗口服务为管理员

+0

您是否尝试过运行的启动服务为管理员应用程序? – Alioza

+0

但这两个应用程序都是windows服务。我如何以管理员身份运行我的Windows服务。 –

+0

我只是在我的app.manifest文件中添加了这个标签它能正常工作... –

回答

2

我只是在我的app.manifest文件中添加此标记 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> 它的工作原理...

0

让分裂到一切步骤

  1. 添加文件“应用程序清单文件”给你根目录下
  2. 选择项目属性
  3. 在应用程序选项卡中的recources组发现舱单场
  4. 粘贴您新的清单文件名。也许“app.manifest”
  5. 填补这个信息

所有信息文件可以根据你的情况是有点不同。 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>