1

I have the following code to check if a Windows service exists but it keeps throwing an exception if the service does not exist.

Dim controller As New ServiceController("test") If controller.Status = Nothing Then Label2.Text = "" Else ListBox1.Items.Add("Service found") End If 

What I would like is do nothing if not found and add to listbox if found.

2
  • (1) What is the exception? (2) Is your code running as an administrator? Querying service status may be a restricted operation. Commented May 28, 2014 at 21:51
  • Hi, Exception is "service not found" and I am running in admin mode Commented May 28, 2014 at 22:21

4 Answers 4

4

That may be by design since ServiceController is meant to "connect to and control the behavior of existing services" (c.f. here). Reading here the Status property is supposed to throw an InvalidOperationException if the service does not exist.

What you could do is use the GetServices() method to list the services on the machine and see if the thing you are looking for exists - perhaps something like:

Dim servicesButNotDevices As ServiceController() = ServiceController.GetServices() For Each service As ServiceController In servicesButNotDevices If service.ServiceName = "my service name" Then 'May also use DispalyName property depending on your use case 'Put in list box Exit For End If Next 

If you didn't want to catch and handle the exception

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help. I tried this but it didn't seem to pick up services that do exist?
@DaE there may be a difference between the ServiceName and the DisplayName properties depending on what you're searching for. For example, running this on my machine I have a service with a ServiceName of "AdobeARMservice" but it has a DisplayName of "Adobe Acrobat Update Service"...so you might want to switch properties depending on whether you're looking for the friendly name of the service or not...
2

According to the ServiceController documentation, this is the expected behavior. If the service exists, the constructor makes a ServiceController instance that provides an interface to control the service. If the service does not exist, there is nothing to do so the constructor throws an ArgumentException.

The following code should do what you need:

Try Dim controller As New ServiceController("test") ListBox1.Items.Add("Service found") Catch ex As ArgumentException Label2.Text = "" End Try 

1 Comment

Thanks for your help. I did try this but it seems to add "service found" with any value added to the ServiceController("test")
1

Thanks for everyones input on this.

I used the idea from NKVU but tweaked it a little:

For Each service As ServiceController In ServiceController.GetServices() Dim serviceName As String = service.ServiceName If serviceName = "mmg" Then ListBox1.Items.Add(serviceName) End If Next 

Comments

1
Public Shared Function GetService() As ServiceProcess.ServiceController Try Dim Services = (From f In ServiceProcess.ServiceController.GetServices Select f Where f.ServiceName = "BilgiSoft").ToList If Services.Count = 1 Then Return Services(0) Exit Function Else Return Nothing Exit Function End If Catch ex As Exception Return Nothing Exit Function End Try End Function 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.