Detect Services By Name

evad4682

Well-known member
Joined
Nov 7, 2005
Messages
55
Programming Experience
Beginner
Hello all

Does anybody know how to detect if a service exists by name or how to detect if a specific service exists at all?

Thanks in advance
 

evad4682

Well-known member
Joined
Nov 7, 2005
Messages
55
Programming Experience
Beginner
Thanks guys.

John, using what you had suggested, this is what I tried.
VB.NET:
[/COLOR]
[COLOR=black]dim s as string[/COLOR]
[COLOR=black]dim pass as boolean[/COLOR]
[COLOR=black]dim arr as array = System.ServiceProcess.ServiceController.GetServices()[/COLOR]
[COLOR=black]Dim i as integer = 0[/COLOR]
[COLOR=black] [/COLOR]
[COLOR=black]do until i = arr.length[/COLOR]
[COLOR=black]s=arr.tostring[/COLOR]
[COLOR=black]if s = "Service Name" then[/COLOR]
[COLOR=black]pass = true[/COLOR]
[COLOR=black]exit do[/COLOR]
[COLOR=black]end if[/COLOR]
[COLOR=black]i = i +1[/COLOR]
[COLOR=black]loop[/COLOR]
[COLOR=black] [/COLOR]
[COLOR=black]if i = arr.length then[/COLOR]
[COLOR=black]pass = false[/COLOR]
[COLOR=black]end if[/COLOR]
[COLOR=black]

As you probably know 'arr.tostring' does not return a service name. It returns "System.ServiceProcess.ServiceController[]". What does .GetServices() populate my array with? Can I test the elements in that array against a string?

Thanks everyone
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,438
Location
Norway
Programming Experience
10+
GetServices method returns an array of type ServiceController.
VB.NET:
Dim sc As System.ServiceProcess.ServiceController
For Each sc In System.ServiceProcess.ServiceController.GetServices()
    MsgBox(sc.ServiceName)
Next
 

evad4682

Well-known member
Joined
Nov 7, 2005
Messages
55
Programming Experience
Beginner
Thanks John, I will try that out!

This is what I started playing around with...
VB.NET:
 Dim gogo As Boolean = False
        Dim op As String
        If Directory.Exists("\\" & compname & "\c$\windows\system32") Then
            op = "xp"
        Else
            op = "2k"
        End If
        Dim myconn As New System.Management.ConnectionOptions
        With myconn
            .Impersonation = System.Management.ImpersonationLevel.Impersonate
            If op = "xp" Then
                .Authentication = AuthenticationLevel.Packet
            Else
                .Authentication = AuthenticationLevel.Connect
            End If
        End With
        Dim scope As System.Management.ManagementScope = New System.Management.ManagementScope("\\" & compname & "\root\cimv2", myconn)
        scope.Connect()
        Dim moOS As ManagementObjectSearcher = New ManagementObjectSearcher(scope.Path.ToString, "SELECT displayName FROM Win32_Service")
        Dim moDS As ManagementObject
        Dim dss As String
        For Each moDS In moOS.Get
            dss = moDS("DisplayName").ToString
            If dss = "Service Name" Then
                gogo = True
                Exit For
            End If
        Next

Your WMI suggestion pointed me in a new direction.

Thanks again!
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,438
Location
Norway
Programming Experience
10+
You can narrow the WMI search with WHERE clause.
VB.NET:
SELECT DisplayName FROM Win32_Service WHERE DisplayName = 'something'
ServiceController.GetServices("remote") can also be used against remote machines.
 
Top Bottom