Start and Stop a Windows Service Programmatically

Problem

You want to start or stop a Windows service that is installed on the current computer.

Solution

Use the Stop and Start methods of the ServiceController class.

Discussion

Recipe Retrieve Information About Installed Windows Services shows the properties of the ServiceController class. The ServiceController class also provides methods such as Start, Stop, Pause, and Continue.

As an example, here’s how you might start a service programmatically:

Dim TestService As New ServiceController("TestService")
TestService.Start()

The ServiceController class methods allow you to start and stop services and monitor their status, but they won’t allow you to interact in an application-specific way. In other words, ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.