Check if Service Exists?

L

Lucas Tam

Hi all,

If I create a serviceController like:

srvController = New ServiceController("Fubar")

serviceController will be created regardless if the service exists or not.
It will only throw an exception if I access the properties of the
serviceController.

Is there a way to check that the service actually exists before creating
it?

Thanks.
 
G

Guest

DOn't know if this the correct way or not but it's what one of our programs
is doing to see if a Service is installed.

'
' Check to see if VNC is installed
'
Dim scServices() = ServiceController.GetServices()
Dim i As Integer = 0

While i < scServices.Length
'MessageBox.Show(scServices(i).DisplayName)
If scServices(i).DisplayName = "VNC Server" Then
m_bVNC = True
End If
i = i + 1
End While


If m_bVNC Then

'
' Check to see if VNC is already running, If so indecate
that VNC is running
'
Try

If m_VNC Is Nothing Then
m_VNC = New ServiceController("VNC Server")
End If


m_VNC.Refresh()

If m_VNC.Status <> ServiceControllerStatus.Stopped Then
tbVnc.ImageIndex = 7
tbVnc.Text = "VNC - ON"
End If

Catch exp As Exception
MsgBox(m_VNC.ServiceName & " is in state:" &
m_VNC.Status.ToString() & vbNewLine & _
"Could not start service")

Finally
m_VNC.Dispose()
m_VNC = Nothing
End Try

Else
tbVnc.Enabled = False

End If
 
L

Lucas Tam

DOn't know if this the correct way or not but it's what one of our
programs is doing to see if a Service is installed.

'
' Check to see if VNC is installed
'
Dim scServices() = ServiceController.GetServices()
Dim i As Integer = 0

While i < scServices.Length
'MessageBox.Show(scServices(i).DisplayName)
If scServices(i).DisplayName = "VNC Server" Then
m_bVNC = True
End If
i = i + 1
End While

Thank you - I was hoping to avoid looping through all my services. I was
sort of expecting that .NET would throw an error if it tried to create
an non-existing service.

i.e. New ServiceController("NoSuchService")

But it doesn't.

In anycase, the GetServices method way will work just fine for me.

Thank you : )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top