-1

I have got a comma separated List of the service names which I need to stop but before that I want to check if all of them are present on the System. How can I get the name of those services which are not present using LINQ.

I looked into this one but it checks for only one service Name not the complete List. Check if a service exists on a particular machine without using exception handling

Community
  • 1
  • 1
  • I guess you can get the List of services and then split your string to get the if it is contained in the services list or not – Pankaj Mar 08 '16 at 15:25
  • Try coding something, then come back when you run up against an issue. –  Mar 08 '16 at 15:31

1 Answers1

1
 ServiceController[] services = ServiceController.GetServices();
 var availableServices = services.Select(service => service.ServiceName).ToList();

 // This is the comma separated List
 string commaSeperatedList = "Service1,Service2,Service3";
 var servicesToStop = commaSeperatedList.Split(',');
 List<string> notPresentServices = servicesToStop.Where(serviceToStop => availableServices.Contains(serviceToStop) == false).ToList();

We are just checking which of the items from one list are not contained in another list.

Pankaj
  • 2,618
  • 3
  • 25
  • 47