New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: VB.NET

Windows Service Installer AutoStart

202 Views
Copy Code Show/Hide Line Numbers
Public Class ProjectInstaller
 
    Private Const LOG_SOURCE As String = "Log"
    Private Const LOG_LOG As String = "Log"
    Private _mainEventLog As EventLog
 
    Public Sub New()
        MyBase.New()
 
        'This call is required by the Component Designer.
        InitializeComponent()
 
        If Not EventLog.SourceExists(LOG_SOURCE) Then
            EventLog.CreateEventSource(LOG_SOURCE, LOG_LOG)
        End If
        'Add initialization code after the call to InitializeComponent
 
    End Sub
 
    Private Sub ProjectInstaller_BeforeUninstall(ByVal sender As Object, ByVal e As System.Configuration.Install.InstallEventArgs) Handles Me.BeforeUninstall
        Dim servicename As String = ""
        Dim services As System.ServiceProcess.ServiceController()
        services = System.ServiceProcess.ServiceController.GetServices()
        Dim Found As Boolean = False
        For Each sc As ServiceController In services
            If String.Compare(servicename, sc.ServiceName, True) = 0 Then
                Found = True
            End If
        Next
        If Found Then
            Dim Controller As New ServiceController(servicename)
            If Controller.Status = ServiceControllerStatus.Running Then
                Controller.Stop()
            End If
        End If
    End Sub
 
    Private Sub ProjectInstaller_Committed(ByVal sender As Object, ByVal e As System.Configuration.Install.InstallEventArgs) Handles Me.Committed
        Dim servicename As String = ""
        Dim services As System.ServiceProcess.ServiceController()
        services = System.ServiceProcess.ServiceController.GetServices()
        Dim Found As Boolean = False
        For Each sc As ServiceController In services
            If String.Compare(servicename, sc.ServiceName, True) = 0 Then
                Found = True
            End If
        Next
        If Found Then
            Dim Controller As New ServiceController(servicename)
            If Controller.Status = ServiceControllerStatus.Stopped Then
                Controller.Start()
            End If
        End If
    End Sub
 
End Class 
by jaredmroberts
  October 08, 2009 @ 11:14am
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies


If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate