In the System.ServiceProcess
namespace, is there any kind of enumeration or other direct member to determine a Service's Start Type (Auto, Delayed Auto, On Demand, Disabled) of a ServiceController
?
The idea is to use an available member of that namespace (or other namespace) of the .NET framework class library to determine that thing, instead of looking into the OS registry or WMI for the service's start type, because I could do that, I'm only asking if the .NET framework exposes an easier way to determine that thing.
Pseudo-Code written in VB.Net but I could manage a C# approach too:
Public Shared Function GetStartType(ByVal svcName As String) As ServiceControllerStatus
Dim svc As ServiceController = (From service As ServiceController In ServiceController.GetServices()
Where service.ServiceName.Equals(svcName, StringComparison.OrdinalIgnoreCase)
).FirstOrDefault
If svc Is Nothing Then
Throw New ArgumentException("Any service found with the specified name.", "svcName")
Else
Using svc
' Note that StartTypeEnumValue does not exists.
Return svc.StartTypeEnumValue
End Using
End If
End Function
The ServiceStartMode is used by the service installer to indicate whether the new service should be disabled at system startup, whether the system should start the service automatically at system startup, or whether the service should be started manually by a user or application.
This example shows how to change a service's startup type. Set-Service uses the Name parameter to specify the service's name, BITS. The StartupType parameter sets the service to Automatic. Get-Service uses the Name parameter to specify the BITS service and sends the object down the pipeline.
If possible, set your project target .NET framework to 4.6.1 or higher.
The class ServiceController
now has a property StartType
https://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller(v=vs.110).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With