Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Windows service start type?

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
like image 264
ElektroStudios Avatar asked Apr 14 '15 06:04

ElektroStudios


People also ask

What is service startup type?

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.

How do I change the startup type of a service in PowerShell?

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.


1 Answers

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

like image 156
Rubanov Avatar answered Sep 27 '22 16:09

Rubanov