Leonardo Butelli
asked on
ServiceController STARTTYPE
I'm writing a program to monitor all the services on a specific pc.
this is part of my code :
....
Dim lista As ServiceController()
lista = ServiceController.GetServi ces(PCNAME )
For Each s As ServiceController In lista
i got :
s.Servicename, s.displayname, s.status etc etc
next
What i need is the Starttype of the service. If i go in debug , i can see the "s.Starttype" to be (Automatic, Manual, Disabled)...etc tec but writing my code i got error when i write down the "s.starttype" i receive error that this "starttype" doesn't exist.
Any help ??
thanks a lot
this is part of my code :
....
Dim lista As ServiceController()
lista = ServiceController.GetServi
For Each s As ServiceController In lista
i got :
s.Servicename, s.displayname, s.status etc etc
next
What i need is the Starttype of the service. If i go in debug , i can see the "s.Starttype" to be (Automatic, Manual, Disabled)...etc tec but writing my code i got error when i write down the "s.starttype" i receive error that this "starttype" doesn't exist.
Any help ??
thanks a lot
ASKER
Hi, thanks a lot for the quick answer.
I don't need to START or STOP a Services.
What i need to know is the Startup type of that service if it is : "Automatic, Manual or Disabled"
Thanks a lot
I don't need to START or STOP a Services.
What i need to know is the Startup type of that service if it is : "Automatic, Manual or Disabled"
Thanks a lot
Here give this a try. Don't forget to Imports System.Management
Public Shared Function GetStartType(ByVal svcName As String) As String
Dim startMode As String = String.Empty
Dim filter As String = String.Format("SELECT StartMode FROM Win32_Service WHERE Name = '{0}'", svcName)
Dim svc As ManagementObjectSearcher = New ManagementObjectSearcher(filter)
If svc Is Nothing Then
Throw New ArgumentException("Any service found with the specified name.", paramName:="svcName")
Else
Try
Dim services As ManagementObjectCollection = svc.Get()
For Each service As ManagementObject In services
startMode = service.GetPropertyValue("StartMode").ToString()
Next
Catch ex As Exception
' Do something if needed
End Try
End If
Return StartMode
End Function
ASKER
Thanks a lot. I will try and let you know !!!!!!!!!
Again thanks
Again thanks
No problem, anytime. If you need more help let me know.
ASKER
PERFECT !!!! Thanks a lot.
Why the hell the method i used doesn't work and need to go with WMI ? (Starttype)
For Each s As ServiceController In lista
s.Servicename
s.displayname
s.status
etc etc
s.Starttype
next
Why the hell the method i used doesn't work and need to go with WMI ? (Starttype)
For Each s As ServiceController In lista
s.Servicename
s.displayname
s.status
etc etc
s.Starttype
next
You could also pull it from the Registry as well.
Public Shared Function GetStartMode(ByVal svcName As String) As SvcStartMode
Dim reg As RegistryKey = Nothing
Dim startModeValue As Integer = 0
Dim delayedAutoStartValue As Integer = 0
Try
reg = Registry.LocalMachine.
OpenSubKey("SYSTEM\CurrentControlSet\Services\" & svcName,
writable:=False)
If reg Is Nothing Then
Throw New ArgumentException("Any service found with the specified name.",
paramName:="svcName")
Else
startModeValue = Convert.ToInt32(reg.GetValue("Start",
defaultValue:=-1))
delayedAutoStartValue = Convert.ToInt32(reg.GetValue("DelayedAutoStart",
defaultValue:=0))
If startModeValue = -1 Then
Throw New Exception(String.Format(
"Registry value ""Start"" not found for service '{0}'.",
svcName))
Return SvcStartMode.Undefinied
Else
Return DirectCast(
[Enum].Parse(GetType(SvcStartMode),
(startModeValue - delayedAutoStartValue).ToString),
SvcStartMode)
End If
End If
Catch ex As Exception
Throw
Finally
If reg IsNot Nothing Then
reg.Dispose()
End If
End Try
End Function
Also you could change your code and see if it works.
Change
To
Change
lista = ServiceController.GetServices(PCNAME)
To
lista = ServiceProcess.ServiceController.GetServices
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Start Service:
Open in new window
Stop Service:
Open in new window