Skip to content

Instantly share code, notes, and snippets.

@kopiro
Created November 26, 2018 08:10
Show Gist options
  • Save kopiro/860b005e11db225566e8a561ab069608 to your computer and use it in GitHub Desktop.
Save kopiro/860b005e11db225566e8a561ab069608 to your computer and use it in GitHub Desktop.
Auto Update VB.NET Compact 3.5 App
Public Class AutoUpdater
Public newVersion As String
Public urlOfCab As String
Public credentials As Net.NetworkCredential
Public askDest As Boolean = False
Public deleteCab As Boolean = True
Public useUI As Boolean = False
Public permitUninstall As Boolean = False
Public installTimeout As Integer = 10000
Public Enum AutoUpdateResult
NoNeedUpdate
DownloadError
InstallError
Ok
End Enum
Public Sub New(ByVal newVersion As String, ByVal urlOfCab As String, Optional ByVal autoRun As Boolean = False)
Me.newVersion = newVersion
Me.urlOfCab = urlOfCab
If autoRun Then
Run()
End If
End Sub
' Download the CAB file into a temporary file and return that file
Public Function DownloadCABInTemp() As String
Dim req As Net.HttpWebRequest = Net.WebRequest.Create(urlOfCab)
If credentials IsNot Nothing Then
Dim credCache As New Net.CredentialCache
credCache.Add(New Uri(urlOfCab), "Basic", credentials)
req.Credentials = credCache
End If
Dim res As Net.HttpWebResponse = req.GetResponse()
Dim dataStream As IO.Stream = res.GetResponseStream()
Dim fileName As String = IO.Path.GetTempFileName() + ".cab"
Dim fileStream As IO.FileStream = IO.File.Create(fileName)
Dim buffer(81920) As Byte
Dim read As Integer
While read = dataStream.Read(buffer, 0, buffer.Length)
fileStream.Write(buffer, 0, read)
End While
Return fileName
End Function
Public Function InstallCAB(ByVal fileName As String) As Diagnostics.Process
Dim wceLoadString As String = ""
If askDest Then
wceLoadString += "/askdest"
Else
wceLoadString += "/noaskdest"
End If
If deleteCab Then
wceLoadString += " /delete 1"
Else
wceLoadString += " /delete 1"
End If
If Not useUI Then
wceLoadString += " /noui"
End If
If Not permitUninstall Then
wceLoadString += " /nouninstall"
End If
wceLoadString += " " + fileName
Dim proc As New Diagnostics.Process
proc = Diagnostics.Process.Start("wceload.exe", wceLoadString)
proc.WaitForExit(installTimeout)
If proc.ExitCode <> 0 Then
Throw New Exception(proc.ToString())
End If
Return proc
End Function
Public Function CheckVersion() As Integer
Return Reflection.Assembly.GetExecutingAssembly().GetName().Version.CompareTo(New Version(newVersion))
End Function
Public Function Run() As AutoUpdateResult
If CheckVersion() >= 0 Then
Return AutoUpdateResult.NoNeedUpdate
End If
Dim downloadedCAB As String
Try
downloadedCAB = DownloadCABInTemp()
Catch ex As Exception
MsgBox("Error in download CAB: " + ex.Message)
Return AutoUpdateResult.DownloadError
End Try
Try
InstallCAB(downloadedCAB)
Catch ex As Exception
MsgBox("Error in install CAB: " + ex.Message)
Return AutoUpdateResult.InstallError
End Try
Return AutoUpdateResult.Ok
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment