Start or Stop a Windows Service using VB.NET

How to start or stop a Windows Service using VB.NET
  • 33684

We can start, stop, pause, continue and refresh a service using Start, Stop, Pause, Continue, and Refresh methods.  Close method disconnects this ServiceController instance from the service and frees all the resources that the instance allocated.

The following code snippet checks if a service is stopped, start it; otherwise stop it.

Let's say, you have a service named "MyServiceName". First you create a ServiceController object and then call its Start or Stop methods to start and stop a windows service.

    Private Sub StartStop()

        Dim service As ServiceController = New ServiceController("MyServiceName")

        If ((service.Status.Equals(ServiceControllerStatus.Stopped)) Or

            (service.Status.Equals(ServiceControllerStatus.StopPending))) Then

            service.Start()

        Else

            service.Stop()

        End If

    End Sub

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.