Imports System.ComponentModel Imports System.ServiceProcess Imports System.Configuration.Install Imports System.Reflection Public Class WinService ''' ''' 描述服务的状态 ''' Enum ServiceStatusEnum Unknown Nonentity Exists StopPending Stopped StartPending Started End Enum ''' ''' 获取枚举描述特性 ''' ''' 需要获取特性的枚举值 ''' 枚举描述特性 Public Shared Function GetEnumDescription(enumValue As [Enum]) As String Dim fi As FieldInfo = enumValue.GetType().GetField(enumValue.ToString()) Dim attr() As DescriptionAttribute = DirectCast(fi.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute()) If attr.Length > 0 Then Return attr(0).Description Else Return enumValue.ToString() End If End Function ''' ''' 检测服务是否存在 ''' ''' 需要检测的服务名称 ''' 服务是否存在 Public Shared Function ServicesExists(serviceName As String) As Boolean Dim services() As ServiceController = ServiceController.GetServices For Each service As ServiceController In services If service.ServiceName = serviceName Then Return True End If Next Return False End Function ''' ''' 获取服务的状态 ''' ''' 服务名称 ''' Public Shared Function ServicesStatus(serviceName As String) As ServiceStatusEnum Dim status As ServiceStatusEnum Try If ServicesExists(serviceName) Then status = ServiceStatusEnum.Exists Try Dim service As New ServiceController(serviceName) If service.Status = ServiceControllerStatus.Running Then status = ServiceStatusEnum.Started ElseIf service.Status = ServiceControllerStatus.StartPending OrElse service.Status = ServiceControllerStatus.ContinuePending Then status = ServiceStatusEnum.StartPending ElseIf service.Status = ServiceControllerStatus.StopPending OrElse service.Status = ServiceControllerStatus.PausePending Then status = ServiceStatusEnum.StopPending Else status = ServiceStatusEnum.Stopped End If Catch ex As Exception Console.WriteLine($"ServicesStatus Error:{ex.Message}") End Try Else status = ServiceStatusEnum.Nonentity End If Catch ex As Exception status = ServiceStatusEnum.Unknown Console.WriteLine($"Check ServicesStatus Error:{ex.Message}") End Try Return status End Function ''' ''' 安装服务 ''' ''' 服务程序所在路径 ''' 服务状态,未知字段 Public Shared Function InstallService(filePath As String, savedState As IDictionary) As Boolean Try Dim app As AppDomain = AppDomain.CreateDomain("MyDomain") Dim assName As String = GetType(AssemblyInstaller).Assembly.FullName Dim fullName As String = GetType(AssemblyInstaller).FullName Using installer As AssemblyInstaller = CType(app.CreateInstanceAndUnwrap(assName, fullName), AssemblyInstaller) installer.UseNewContext = True installer.Path = filePath installer.Install(savedState) installer.Commit(savedState) installer.Dispose() End Using AppDomain.Unload(app) GC.Collect() Catch ex As Exception Console.WriteLine($"InstallService Error:{ex.Message}") Return False End Try Return True End Function ''' ''' 卸载服务 ''' ''' 服务程序所在路径 ''' 服务状态,未知字段 Public Shared Function UnInstallService(filePath As String, savedState As IDictionary) As Boolean Try Dim app As AppDomain = AppDomain.CreateDomain("MyDomain") Dim assName As String = GetType(AssemblyInstaller).Assembly.FullName Dim fullName As String = GetType(AssemblyInstaller).FullName Using installer As AssemblyInstaller = CType(app.CreateInstanceAndUnwrap(assName, fullName), AssemblyInstaller) installer.UseNewContext = True installer.Path = filePath installer.Uninstall(savedState) installer.Dispose() End Using AppDomain.Unload(app) GC.Collect() Catch ex As Exception Console.WriteLine($"UnInstallService Error:{ex.Message}") Return False End Try Return True End Function '''' '''' 安装服务 '''' '''' 服务程序所在路径 '''' 服务状态,未知字段 'Public Shared Sub InstallService(filePath As String, savedState As IDictionary) ' Using installer As New AssemblyInstaller ' installer.UseNewContext = True ' installer.Path = filePath ' installer.Install(savedState) ' installer.Commit(savedState) ' installer.Dispose() ' End Using 'End Sub '''' '''' 卸载服务 '''' '''' 服务程序所在路径 '''' 服务状态,未知字段 'Public Shared Function UnInstallService(filePath As String, savedState As IDictionary) As Boolean ' Try ' Using installer As New AssemblyInstaller ' installer.UseNewContext = True ' installer.Path = filePath ' installer.Uninstall(savedState) ' End Using ' Catch ex As Exception ' Console.WriteLine($"InstallService Error:{ex.Message}") ' Return False ' End Try ' Return True 'End Function ''' ''' 开启服务 ''' ''' 服务名称 Public Shared Sub StartService(serviceName As String) Using service As ServiceController = New ServiceController(serviceName) If service.Status <> ServiceControllerStatus.Running OrElse service.Status <> ServiceControllerStatus.StartPending OrElse service.Status <> ServiceControllerStatus.ContinuePending Then service.Start() End If End Using End Sub ''' ''' 关闭服务 ''' ''' 服务名称 ''' 关闭服务结果 Public Shared Function StopService(serviceName As String) As Boolean Try Using service As ServiceController = New ServiceController(serviceName) If service.Status = ServiceControllerStatus.Running OrElse service.Status = ServiceControllerStatus.StartPending OrElse service.Status = ServiceControllerStatus.ContinuePending Then service.Stop() End If End Using Catch ex As Exception Console.WriteLine($"StopService Error:{ex.Message}") Return False End Try Return True End Function ''' ''' 重启服务 ''' ''' 服务名称 ''' Public Shared Function RestartService(serviceName As String) As Boolean Try Using service As ServiceController = New ServiceController(serviceName) If service.Status = ServiceControllerStatus.Running OrElse service.Status = ServiceControllerStatus.StartPending OrElse service.Status = ServiceControllerStatus.ContinuePending Then service.Stop() While service.Status = ServiceControllerStatus.Running AndAlso service.Status = ServiceControllerStatus.StopPending service.Refresh() Threading.Thread.Sleep(20) End While End If Threading.Thread.Sleep(100) service.Start() End Using Catch ex As Exception Console.WriteLine($"RestartService:{ex.Message}") Return False End Try Return True End Function End Class