Programmatically Remote Control a Windows Service

I just thought this was cool, start and stop remote services like so:

System.ServiceProcess.ServiceController sc2 =

new System.ServiceProcess.ServiceController(“ServiceName”, “MACHINE_NAME”);
if
(sc2.Status.Equals(System.ServiceProcess.ServiceControllerStatus.Running))
{
  sc2.Stop();
}

Today’ music tip: Run, don’t walk to CDNOW to get the new Outkast album… Speakerboxxx/The Love Below.  The track “Hey Ya” is soooooo good.  And only $17 for two separate albums!

About Brendan Tompkins

Brendan runs CodeBetter.Com and Devlicio.Us. He is a former MVP for Microsoft .NET and is president of Port Technology Services, a partner with Port Solution Integrators a provider of hardware and software integration services for the transportation and logistics industry.
This entry was posted in .NET General. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://google.com NewEra

    [url="http://buy-online-viagra.nexo.com"]BUY VIAGRA ONLINE[/url]

  • Sagar

    I am facing the following problem

    When I give start() service
    I get the exception as, “it cant start service on ‘.’ computer.”

    Please help… thanks in advance

  • venky

    Hi guys here also the same problem .
    i tried the same service controller it works perfectly with XP machine.
    but in the case of windows server 2003 sp2 (don’t know about sp1 case)

    log is here

    LogType:ErrorMessage ErrorMessage:Cannot open Servicename service on computer ‘Competername’.;InnerExcpetion:Access is denied ExceptionDetails: at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
    at System.ServiceProcess.ServiceController.Start(String[] args)
    at System.ServiceProcess.ServiceController.Start()
    at ListenerServicesManagement.startService(String serviceName, String machineName, String userName) InnerException:System.ComponentModel.Win32Exception: Access is denied UserName:admin LogTime:1/12/2009 4:55:48 PM

    Can anyone help about this issue…. it is really urgent

  • Kartik

    You are saying that it connects remote machine to manage service which is running on it. So which port it use to connect remote machine.

  • default

    I’m only replying to this because it iss the first google hit for the exception… :-)

    Check permissions on the account trying to manage the service. That should clear everything up

  • abc

    Nishi, what do you mean by ‘When impersnate was removed’ ? Please elaborate.

  • renuka

    when i give start() thru program ,

    it says , it cant start service on ‘.’ computer.

    How can i soleve this?

  • nishi

    Got it done.

    When impersonate was removed , it worked fine.

  • nishi

    MACHINE_NAME – >

    Boss , i am reasonably a fresher

    default , machine name comes as ‘.’

    for a service controller object.

    So , when i give start() thru program ,

    it says , it cant start service on ‘.’ computer.

    How can i soleve this?

    Thanks

    in advance

  • bvn

    Hello there,

    We have it working as suggested in Windows XP.

    How come the code provided does not work on Windows 2003 Server with .NET 1.1.4322?

    The following error message returned:

    System.Web.Services.Protocols.SoapException: Server was unable to process request. —> System.InvalidOperationException: Cannot open ERPPartySubscriber service on computer ‘.’. —> System.ComponentModel.Win32Exception: Access is denied — End of inner exception stack trace — at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess) at System.ServiceProcess.ServiceController.Start(String[] args) at System.ServiceProcess.ServiceController.Start() at Intuit.XI.XIServiceController.WindowsServiceController.StartService(String serviceName) in c:\integration platform 2.0\master\xiservicecontroller\windowsservicecontroller.asmx.cs:line 242 — End of inner exception

    Any clues?

    Thank you,

    B. Nguyen

  • Rob Reagan

    You have to include a reference to the System.ServiceProcess library before you can see the ServiceController class.

    Rob

  • ssr

    How come my app doesnt recognize the ServiceProcess namespace. I get the messages; "ServiceProcess is not a member of System" and "System.ServiceProcess.ServiceController is not defined. "

    thanks.