Get Windows startup type?

Is there any numbering or other direct member in the System.ServiceProcess namespace to determine the type of service start (automatic, delayed auto, on demand, disabled) ServiceController ?

The idea is to use the available member of this namespace (or another namespace) of the .NET Framework class library to define this thing, instead of looking in the OS or WMI registry for the type of service starting, because I could do what I just asking if the .NET Framework provides an easier way to define this thing.

Pseudocode written in VB.Net, but I could also use the C # approach:

 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 
+6
source share
4 answers

Since I'm not quite sure about the answer to my main question (a member within the .net framework class library that can get this information), I developed this registry approach, which is at least much faster than messing with WMI and can also determine delayed startup service.

First enter a custom enumeration to extend the actual ServiceStartMode enumeration:

 ''' <summary> ''' Indicates the start mode of a service. ''' </summary> Public Enum SvcStartMode As Integer ''' <summary> ''' Indicates that the service has not a start mode defined. ''' Since a service should have a start mode defined, this means an error occured retrieving the start mode. ''' </summary> Undefinied = 0 ''' <summary> ''' Indicates that the service is to be started (or was started) by the operating system, at system start-up. ''' The service is started after other auto-start services are started plus a short delay. ''' </summary> AutomaticDelayed = 1 ''' <summary> ''' Indicates that the service is to be started (or was started) by the operating system, at system start-up. ''' If an automatically started service depends on a manually started service, ''' the manually started service is also started automatically at system startup. ''' </summary> Automatic = 2 'ServiceStartMode.Automatic ''' <summary> ''' Indicates that the service is started only manually, ''' by a user (using the Service Control Manager) or by an application. ''' </summary> Manual = 3 'ServiceStartMode.Manual ''' <summary> ''' Indicates that the service is disabled, so that it cannot be started by a user or application. ''' </summary> Disabled = 4 ' ServiceStartMode.Disabled End Enum 

Secondly, this function:

 ''' <summary> ''' Gets the start mode of a service. ''' </summary> ''' <param name="svcName">The service name.</param> ''' <returns>The service start mode.</returns> ''' <exception cref="ArgumentException"> ''' Any service found with the specified name. ''' </exception> ''' <exception cref="Exception"> ''' Registry value "Start" not found for service. ''' </exception> ''' <exception cref="Exception"> ''' Registry value "DelayedAutoStart" not found for service. ''' </exception> 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 
+3
source

You can use WMI and ManagementObject to achieve this, based on C # - Get service startup type (Windows) .

Something like this (based on the code in a related article). The original example is in C #, so I tried to quickly switch to VB.NET on the fly, but the syntax may be a little wrong. I also changed the type of the returned method to String , as I was not sure what you wanted to do with the value after receiving it.

Remember to add 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 
+4
source

If possible, install the target .NET platform for the platform in 4.6.1. The ServiceController class now has the StartType property

https://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller(v=vs.110).aspx

+3
source

Using
Dim SingleSvc As Object

 Dim listaSvcs() As ServiceProcess.ServiceController Dim SingleSvc As Object iniservice: listaSvcs = ServiceProcess.ServiceController.GetServices Try For Each SingleSvc In listaSvcs If SingleSvc.ServiceName.IndexOf("postgresql") >= 0 And SingleSvc.StartType.ToString <> "Disabled" Then If SingleSvc.Status <> ServiceProcess.ServiceControllerStatus.Running Then 'MessageBox.Show(SingleSvc.StartType.ToString) SingleSvc.Start() GoTo iniservice End If End If Next Catch ex As Exception MessageBox.Show(ex.Message) End Try 

Launch in Framework 4 Tanks

0
source

Source: https://habr.com/ru/post/985094/


All Articles