My hacky solution is to modify the ProjectInstaller.vb
file so it issues a command to stop and delete the service and then pauses a bit. Probably not as neat from an install perpective as modifying the msi
file, but much more readable/logical to whoever inherits my code.
Note that the RunCommandCom
bit is blatently cribbed from How to run DOS/CMD/Command Prompt commands from VB.NET?
Using this method combined with code from How to automatically start your service after install? you can have the service install experience you want - A service that installs and starts automatically, and will overwrite a currently running service if there is one.
'This works. It leaves the MSI in a state that tells you to reboot the PC, but you really don't need to.
Private Sub ProjectInstaller_BeforeInstall(sender As Object, e As System.Configuration.Install.InstallEventArgs) Handles Me.BeforeInstall
Dim sEchoMessage As String = String.Empty
sEchoMessage &= " & ECHO ****************** Please be patient *******************************"
sEchoMessage &= " & ECHO Pausing to stop and delete the previous version of the following service:"
sEchoMessage &= " & ECHO " & ServiceInstaller1.ServiceName
sEchoMessage &= " & ECHO -------------------------------------------------------------------------------"
sEchoMessage &= " & ECHO After install is complete, you may see a message that says you need to reboot."
sEchoMessage &= " & ECHO You may IGNORE this message - The service will be installed and running."
sEchoMessage &= " & ECHO There is NO Reboot required."
sEchoMessage &= " & ECHO *******************************************************************************"
RunCommandCom("sc stop " & ServiceInstaller1.ServiceName & " & sc delete " & ServiceInstaller1.ServiceName & sEchoMessage, 15000)
End Sub
Private Sub RunCommandCom(command As String, mSecSleepAfterExecution As Integer)
Using p As Process = New Process()
Dim pi As ProcessStartInfo = New ProcessStartInfo()
pi.Arguments = " /K " + command
pi.FileName = "cmd.exe"
p.StartInfo = pi
p.Start()
System.Threading.Thread.Sleep(mSecSleepAfterExecution)
p.CloseMainWindow()
End Using
End Sub