Reputation: 1791
I'm trying to get the status of NetTcpActivator and NetPipeActivator services in C#.
I used this code to list all the services with a ServiceController:
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
string name = service.ServiceName;
string status = service.Status.ToString();
}
I went in debug but I never see the two services.
If I go in my Services in Windows, I do see the two services. If I go in the Registry and look for SYSTEM\CurrentControlSet\services, I do see NetTcpActivator and NetTcpPortSharing. But in the registry, I cannot see the state of the services.
Any idea why I don't see the services with the ServiceController? Any idea how else to get the service status if it wont show in the ServiceController?
Thanks
Upvotes: 1
Views: 799
Reputation: 6304
I would assume you were iterating through all of the services for debugging purposes, but you could use a simple LINQ statement to get the services you need.
I was able to do these and get the services:
var netTcpActivatorService = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == "NetTcpActivator");
var netPipeActivatorService = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == "NetPipeActivator");
Upvotes: 1