vb.net中windows service服务安装、注册、判断服务是否存在、是否启动代码

文章来源:365jz.com     点击数:140    更新时间:2018-01-13 22:32   参与评论
vb.net中,使用ServiceController类控制windows服务,使用之前要先添加引用:System.ServiceProcess,然后在命名空间中引用:using System.ServiceProcess。下面举例获取本机的所有已安装的Windows服务和应用,然后查找某一应用活服务是否已经安装。

vb.net代码:

'安装服务 
    Private Sub InstallService(ByVal stateSaver As IDictionary, ByVal filepath As String)
        Try
            Dim service As ServiceController = New ServiceController("ServiceName")
            If Not ServiceIsExisted("ServiceName") Then
                'Install Service  需引用 Imports System.Configuration.Install
                Dim myAssemblyInstaller As AssemblyInstaller = New AssemblyInstaller
                myAssemblyInstaller.UseNewContext = True
                myAssemblyInstaller.Path = filepath
                myAssemblyInstaller.Install(stateSaver)
                myAssemblyInstaller.Commit(stateSaver)
                myAssemblyInstaller.Dispose()
                '--Start Service 
                service.Start()
            ElseIf ((service.Status <> System.ServiceProcess.ServiceControllerStatus.Running) _
                        AndAlso (service.Status <> System.ServiceProcess.ServiceControllerStatus.StartPending)) Then
                service.Start()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    '卸载windows服务 
    Private Sub UnInstallService(ByVal filepath As String)
        Try
            If ServiceIsExisted("ServiceName") Then
                'UnInstall Service  需引用 Imports System.Configuration.Install
                Dim myAssemblyInstaller As AssemblyInstaller = New AssemblyInstaller
                myAssemblyInstaller.UseNewContext = True
                myAssemblyInstaller.Path = filepath
                myAssemblyInstaller.Uninstall(Nothing)
                myAssemblyInstaller.Dispose()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    '判断window服务是否存在 
    Private Function ServiceIsExisted(ByVal serviceName As String) As Boolean
        Dim services() As ServiceController = ServiceController.GetServices
        For Each s As ServiceController In services
            If (s.ServiceName = serviceName) Then
                Return True
            End If

        Next
        Return False
    End Function

  
    '启动服务 
    Private Function StartService(ByVal serviceName As String) As Boolean
        Dim flag As Boolean = True
        If ServiceIsExisted(serviceName) Then
            Dim service As System.ServiceProcess.ServiceController = New System.ServiceProcess.ServiceController(serviceName)
            If ((service.Status <> System.ServiceProcess.ServiceControllerStatus.Running) _
                        AndAlso (service.Status <> System.ServiceProcess.ServiceControllerStatus.StartPending)) Then
                service.Start()
                Dim i As Integer = 0
                Do While (i < 60)
                    service.Refresh()
                    System.Threading.Thread.Sleep(1000)
                    If (service.Status = System.ServiceProcess.ServiceControllerStatus.Running) Then
                        Exit Do
                    End If

                    If (i = 59) Then
                        flag = False
                    End If

                    i = (i + 1)
                Loop

            End If

        End If

        Return flag
    End Function

    '停止服务
    Private Function StopService(ByVal serviceName As String) As Boolean
        Dim flag As Boolean = True
        If ServiceIsExisted(serviceName) Then
            Dim service As System.ServiceProcess.ServiceController = New System.ServiceProcess.ServiceController(serviceName)
            If (service.Status = System.ServiceProcess.ServiceControllerStatus.Running) Then
                service.Stop()
                Dim i As Integer = 0
                Do While (i < 60)

                    service.Refresh()
                    System.Threading.Thread.Sleep(1000)
                    If (service.Status = System.ServiceProcess.ServiceControllerStatus.Stopped) Then
                        Exit Do
                    End If

                    If (i = 59) Then
                        flag = False
                    End If

                    i = (i + 1)
                Loop

            End If

        End If

        Return flag
    End Function

    '判断某项服务器是否启动
    Public Shared Function IsServiceStart(ByVal serviceName As String) As Boolean
        Dim psc As ServiceController = New ServiceController(serviceName)
        Dim bStartStatus As Boolean = False
        Try
            If Not psc.Status.Equals(ServiceControllerStatus.Stopped) Then
                bStartStatus = True
            End If

            Return bStartStatus
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Function





如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛


发表评论 (140人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码: 点击我更换图片
最新评论
------分隔线----------------------------