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
- Confirm the reference or NuGet package was added to the project that contains the code, and restore/build the project again.
- Check that the package version supports the target framework in the project file.
- Verify the service name in Windows Services; a display name is not always its system name.
- Ensure the application runs on Windows and has permission to query or control the service. Starting and stopping services may require elevated privileges.
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;.