Suresh Kamrushi
Practical guide · C# and Windows services

Fix “The Type or Namespace Name ServiceController Could Not Be Found” in C#

Add the System.ServiceProcess dependency to your project, import its namespace, and safely start or stop a Windows service.

If C# reports that ServiceController or System.ServiceProcess cannot be found, the project is missing the reference that provides the Windows service API. Add the appropriate reference for your target framework, then import the namespace in the source file.

Add the System.ServiceProcess reference

.NET Framework: In Visual Studio, right-click the project, choose Add Reference, and select System.ServiceProcess under the framework assemblies. Click OK.

Modern .NET: Add the System.ServiceProcess.ServiceController NuGet package that matches your target framework. For example, from the project directory:

dotnet add package System.ServiceProcess.ServiceController

After adding the reference or package, import the namespace at the top of the C# file:

using System.ServiceProcess;

Connect to a service

Construct a ServiceController with the service's actual system name. The display name shown in Services may differ from that name. Dispose of the controller when finished; it does not need a null check after construction.

using System;
using System.ServiceProcess;

using var controller = new ServiceController("MyService");

Console.WriteLine($"Service status: {controller.Status}");

Start or stop the service

Check the current status before requesting a transition, and wait with a timeout if the application needs to confirm that the transition completed.

using System;
using System.ServiceProcess;

using var controller = new ServiceController("MyService");

if (controller.Status == ServiceControllerStatus.Stopped)
{
    controller.Start();
    controller.WaitForStatus(
        ServiceControllerStatus.Running,
        TimeSpan.FromSeconds(10));
}

To stop a running service, call Stop() and wait for ServiceControllerStatus.Stopped. Only request transitions the service supports.

If the error persists

Service operations can fail if the service does not exist, the requested transition is invalid, or the caller lacks permissions. Handle and log the relevant exception at the application boundary rather than silently swallowing errors.

In short: Reference System.ServiceProcess in .NET Framework projects or add the System.ServiceProcess.ServiceController package in modern .NET, then add using System.ServiceProcess;.