93 lines
3.7 KiB
VB.net
93 lines
3.7 KiB
VB.net
|
Module Module1
|
|||
|
Dim sw As New System.Diagnostics.Stopwatch
|
|||
|
Sub Main()
|
|||
|
'autoupdater oldfile downloadpath
|
|||
|
Console.BackgroundColor = ConsoleColor.Black
|
|||
|
Console.Title = "Autoupdater"
|
|||
|
sw.Start()
|
|||
|
Try
|
|||
|
'log(Environment.CommandLine)
|
|||
|
If My.Application.CommandLineArgs.Count <> 2 Then
|
|||
|
log("Incorrect number of arguments used, closing")
|
|||
|
Exit Sub
|
|||
|
End If
|
|||
|
|
|||
|
log("Opening temp file for download")
|
|||
|
Dim tempfile As String = FileIO.FileSystem.GetTempFileName()
|
|||
|
log("Temp file: " & tempfile)
|
|||
|
|
|||
|
Dim dlstring, oldfile As String
|
|||
|
oldfile = My.Application.CommandLineArgs(0)
|
|||
|
dlstring = My.Application.CommandLineArgs(1)
|
|||
|
|
|||
|
If FileIO.FileSystem.FileExists(oldfile) Then
|
|||
|
oldfile = FileIO.FileSystem.GetFileInfo(oldfile).FullName
|
|||
|
log("Old file: " & oldfile)
|
|||
|
Else
|
|||
|
Throw New Exception("Couldn't find old file")
|
|||
|
End If
|
|||
|
|
|||
|
Dim processList() As Process
|
|||
|
Dim runningproc As Process = Nothing
|
|||
|
processList = Process.GetProcesses
|
|||
|
For Each x In processList
|
|||
|
Try
|
|||
|
If x.MainModule.FileName = oldfile Then
|
|||
|
log("Old process still running. PID " & x.Id.ToString)
|
|||
|
runningproc = x
|
|||
|
log("Waiting 5s for process to exit...")
|
|||
|
runningproc.WaitForExit(5000)
|
|||
|
|
|||
|
If Not runningproc.HasExited Then
|
|||
|
log("Process still running, attempting to kill gracefully")
|
|||
|
runningproc.CloseMainWindow()
|
|||
|
runningproc.WaitForExit(5000)
|
|||
|
|
|||
|
If Not runningproc.HasExited Then
|
|||
|
log("Process still running, forcefully terminating")
|
|||
|
runningproc.Kill()
|
|||
|
runningproc.WaitForExit(1000)
|
|||
|
|
|||
|
If Not runningproc.HasExited Then
|
|||
|
Throw New Exception("Could not terminate the process")
|
|||
|
End If
|
|||
|
|
|||
|
End If
|
|||
|
|
|||
|
End If
|
|||
|
log("Process exited")
|
|||
|
runningproc.Close()
|
|||
|
Exit For
|
|||
|
End If
|
|||
|
Catch
|
|||
|
End Try
|
|||
|
Next
|
|||
|
|
|||
|
log("Downloading from " & dlstring)
|
|||
|
Dim wc As New Net.WebClient
|
|||
|
wc.DownloadFile(dlstring, tempfile)
|
|||
|
log("New file downloaded, writing over old file")
|
|||
|
FileIO.FileSystem.WriteAllBytes(oldfile, FileIO.FileSystem.ReadAllBytes(tempfile), False)
|
|||
|
log("New file written, deleting old file")
|
|||
|
FileIO.FileSystem.DeleteFile(tempfile)
|
|||
|
log("Success! Starting new file")
|
|||
|
System.Diagnostics.Process.Start(oldfile, "-delupdater""" & Process.GetCurrentProcess.MainModule.FileName & """")
|
|||
|
Catch ex As Exception
|
|||
|
Console.BackgroundColor = ConsoleColor.Red
|
|||
|
log(ex.ToString)
|
|||
|
Console.ReadLine()
|
|||
|
Exit Sub
|
|||
|
End Try
|
|||
|
End Sub
|
|||
|
|
|||
|
Public Sub log(msg As String)
|
|||
|
Console.ForegroundColor = ConsoleColor.Gray
|
|||
|
Console.Write("[")
|
|||
|
Console.ForegroundColor = ConsoleColor.White
|
|||
|
Console.Write("{0,6:0.000}", sw.Elapsed.TotalSeconds)
|
|||
|
Console.ForegroundColor = ConsoleColor.Gray
|
|||
|
Console.WriteLine("] " & msg)
|
|||
|
End Sub
|
|||
|
|
|||
|
End Module
|