Check if a service exists on a particular machine without using exception handling

前端 未结 3 1160
青春惊慌失措
青春惊慌失措 2020-12-15 02:47

Don\'t know if there is a better way to do this, so that is the reason for the question. I can check if a service exists on a particular machine with the following code:

3条回答
  •  情书的邮戳
    2020-12-15 03:16

    Same approach as adrianbanks but a slight more compact code. If your're using LINQ you can use any statement to return what you want. In addition if you are checking on local computer there's no need to give computer name.

    bool DoesServiceExist(string serviceName)
    {
       return ServiceController.GetServices().Any(serviceController => serviceController.ServiceName.Equals(serviceName));
    }
    

提交回复
热议问题