'安装服务
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
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛