开发者

Starting a Window service from My WPF c# is giving "Cannot open SERVICENAME service on computer '.'. "

I am trying to start a service from my wpf Application and i am getting Cannot open SERVICENAME service on computer when i try to start/stop the service....

I have to do this programmatic i cannot change any setting or permissions manually...

ServiceController service = ServiceController.GetServices().FirstOrDefault(i => i.ServiceName.Contains("SERVICENAME"));

            if (service.Status != ServiceControllerStatus.Running)
开发者_StackOverflow            {
                service.Start();//  /// Cannot open SERVICENAME service on computer '.'. 
                service.WaitForStatus(ServiceControllerStatus.Running);
            }


This is probably not an issue if you are running on an XP machine or under the debugger of Visual Studios, but only when you deploy your program on a Windows 7 machine you see the error:

Cannot open < servicename > service on computer '.'.

This is most likely because of the privileges change which occurred since Vista regarding the User Account Control.

You can validate that this is indeed what is causing the issue if you try running the application as administrator and the issue does not occur.

In order to Start or Stop a Windows services you need to run your C# program with administration rights.

This can either be done manually, by right clicking on the application and then click 'Run as administrator'

Or this can be done programmatically by setting your program to always run with administration right if you add the below code in your app.manifest file located in the Properties folder in your project in Solution Explorer:

 <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">
 <!-- UAC Manifest Options
  If you want to change the Windows User Account Control level replace the 
  requestedExecutionLevel node with one of the following.

 <requestedExecutionLevel level="asInvoker" uiAccess="false" />
 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
 <requestedExecutionLevel level="highestAvailable" uiAccess="false" />

  If you want to utilize File and Registry Virtualization for backward 
  compatibility then delete the requestedExecutionLevel node.
 -->
 <requestedExecutionLevel level="asInvoker" 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>

You can find more info on Creating and Embedding an Application Manifest (UAC) here: https://msdn.microsoft.com/en-us/library/bb756929.aspx


I'd guess that you don't have sufficient rights to control services due to UAC. Try running as admininstrator, i.e. elevating your priviliges.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜