7

System.ServiceProcess命名空间中,是否有任何类型的枚举或其他直接成员来确定服务的启动类型(自动、延迟自动、按需、禁用)ServiceController

这个想法是使用 .NET 框架类库的该命名空间(或其他命名空间)的可用成员来确定该事物,而不是查看操作系统注册表或 WMI 以获取服务的启动类型,因为我可以这样做,我'我只是在询问 .NET 框架是否公开了一种更简单的方法来确定那件事。

用 VB.Net 编写的伪代码,但我也可以管理 C# 方法:

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
4

4 回答 4

5

如果可能,请将您的项目目标 .NET 框架设置为 4.6.1 或更高版本。该类ServiceController现在有一个属性StartType

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

于 2016-04-19T09:22:02.433 回答
4

您可以使用 WMI 和ManagementObject来实现这一点,基于C# – Get Startup Type of a Service (Windows)

像这样的东西(基于链接文章中的代码)。原始示例是用 C# 编写的,因此我尝试快速将其快速转换为 VB.NET,但语法可能有点错误。我还将方法返回类型修改为String因为我不确定一旦获得该值你想对它做什么。

不要忘记添加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
于 2015-04-14T07:00:09.653 回答
4

由于我不完全确定我的主要问题的答案(.net 框架类库中的成员能够获取此信息)我开发了这种注册表方法,它至少比弄乱 WMI 快得多,也可以确定服务是否自动启动延迟。

首先是一个自定义枚举来扩展实际的ServiceStartMode枚举:

''' <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

其次,这个功能:

''' <summary>
''' Gets the start mode of a service.
''' </summary>
''' <param name="svcName">The service name.</param>
''' <returns>The service's 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
于 2015-04-14T09:16:07.363 回答
0

使用
Dim SingleSvc 作为对象

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

在 Framework 4 坦克中运行

于 2016-09-26T19:23:44.213 回答