473,466 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Check if Service Exists?

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.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #1
4 14997
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
"Lucas Tam" wrote:
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.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 21 '05 #2
"=?Utf-8?B?RGFycmVsbCBXZXNsZXk=?="
<Da***********@discussions.microsoft.com> wrote in
news:5F**********************************@microsof t.com:
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 : )
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #3
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
"Lucas Tam" wrote:
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.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 21 '05 #4
"=?Utf-8?B?RGFycmVsbCBXZXNsZXk=?="
<Da***********@discussions.microsoft.com> wrote in
news:5F**********************************@microsof t.com:
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 : )
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Raymond | last post by:
Hi All: To find a file exists using the file name, I have tow routings on UNIX system. 1. access(2) 2. lstat(2) This tow function also can do. When the return value is "-1" and errno is...
1
by: Pete | last post by:
Hi, i'm building a datagrid that grabs a documents location out of a database and puts them in as a hyper link column. Ideally i'd love to be able to check the validity of the link and only...
0
by: Lucas Tam | last post by:
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...
1
by: James | last post by:
vb.net 2003 i wrote a windows service that does threading. My codes are a) the service thread will read a list of machine from a text file (machines.txt). b) it then logon using the below...
2
by: shikha tamta | last post by:
Hi!! I have oracle 8i&oracle 9i installed on my machine. I have set the tnsnames.ora file for Oracle 8i also. But whenevr i try to access the oracle 8i it throws the error: ORA 12154 TNS could...
0
by: Herb | last post by:
is there a way in an SQL stored proc to determine if a temp table exists before selecting from it....I have a java app. that is calling a stored proc. i want to check if Java created the table before...
4
by: Mark Berry | last post by:
Hi, I'm working on my "last resort" error block for a web application. If the error occurs after a Request has been made, I want to show the URL. If the Request object is not available, I'll...
0
by: philip.poole | last post by:
Hi everyone, I am settings up some .NET controls on my website and want to create template controls to completely separate the business layer and the presentation layer. However I have...
2
Manikgisl
by: Manikgisl | last post by:
HI. How to check File exists in Web Share C# try { WebRequest request = HttpWebRequest.Create("http://www.microsoft.com/NonExistantFile.aspx"); request.Method = "HEAD"; // Just get...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.