

Imports System.Net
Public Class MainForm
Private WithEvents httpClient As WebClient
Dim DesktopPath As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
Dim CurrentURL As String
Dim FileName As String
Dim FileFormat As String
Private Sub DeletePermanently()
My.Computer.FileSystem.DeleteFile(DesktopPath & "\" & FileName + FileFormat,
Microsoft.VisualBasic.FileIO.RecycleOption.DeletePermanently,
Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)
End Sub
Private Sub DownloadButton_Click(sender As Object, e As EventArgs) Handles DownloadButton.Click
'CurrentURL = "URL.de/Download/ImageName.jpg" : FileName = "ImageName" : CurrentFormat = ".jpg"
httpClient.DownloadFileAsync(New Uri(CurrentURL), DesktopPath & "\" & FileName + FileFormat)
'= ...Desktop\ImageName.jpg
'Download File with Format from URL
End Sub
Private Sub CancelButton_Click(sender As Object, e As EventArgs) Handles CancelButton.Click
httpClient.CancelAsync() 'Cancel the Download
DeletePermanently() 'Delete the faulty file.
End Sub
Private Sub httpClient_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) _
Handles httpClient.DownloadProgressChanged
DownloadProgressBar.Value = e.ProgressPercentage
Dim Bytes As Double = e.BytesReceived / 1000
Dim TotalBytes As Double = e.TotalBytesToReceive / 1000
StatusLabel.Text = _
"""" & FileName + FileFormat & """" & " is downloaded..." & vbNewLine & Bytes & " KB / " & TotalBytes & " KB"
'= "ImageName.jpg" is downloaded... 300 KB / 900 KB
End If
End Sub
End Class
Imports System.Net
Imports System****
Public Class AutoUpdateClass
Public Event UpdatePercent(ByVal nCompleted As Integer)
Public Event DownloadComplete()
Public Event Status(ByVal sStatus As String)
Private sVersion As String
Private VersionURL As String
Private ApplicationURL As String
Private PathOldFile As String
Private _CurrentPath As CurrentPath
Private Structure CurrentPath
Dim Path As String
Dim FileName As String
End Structure
Private _TempPath As TempPath
Private Structure TempPath
Dim Path As String
Dim FileName As String
End Structure
Public Sub New(ByVal sVersionURL As String, _
ByVal sApplicationURL As String, _
ByVal sPathOldFile As String)
VersionURL = sVersionURL
ApplicationURL = sApplicationURL
PathOldFile = sPathOldFile
_CurrentPath.Path = Path.GetDirectoryName(sPathOldFile) & "\"
_CurrentPath.FileName = Path.GetFileName(sPathOldFile)
End Sub
Public Sub Run()
GetVersion(VersionURL)
End Sub
Private Sub GetApplication()
If Not String.IsNullOrEmpty(sVersion) Then
If sVersion <> Application.ProductVersion Then
DownloadApplication(ApplicationURL)
Else
RaiseEvent Status("Version: " & Application.ProductVersion)
End If
Else
'TODO: Error while downloading version, do what you want (You can also raise DownloadComplete event and manage it)
End If
End Sub
#Region "Get version number"
Private Sub GetVersion(ByVal sVersionURL As String)
RaiseEvent Status("Checking for an update...")
Dim wc As New WebClient
AddHandler wc.DownloadStringCompleted, AddressOf DownloadStringCompleted
wc.DownloadStringAsync(New Uri(sVersionURL))
End Sub
Private Sub DownloadStringCompleted(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
If e.Cancelled = False AndAlso e.Error Is Nothing Then
sVersion = e.Result
GetApplication()
Else
End If
End Sub
#End Region
#Region "Download updated exe"
Private Sub DownloadApplication(ByVal sURL As String)
RaiseEvent Status("Dowloading update...")
Dim wc As New WebClient
AddHandler wc.DownloadProgressChanged, AddressOf ProgressChanged
AddHandler wc.DownloadFileCompleted, AddressOf DownloadCompleted
_TempPath.Path = Path.GetTempPath
_TempPath.FileName = _CurrentPath.FileName
wc.DownloadFileAsync(New Uri(sURL), _TempPath.Path & _TempPath.FileName)
End Sub
Private Sub ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
Dim bytesIn As Double = Double.Parse(e.BytesReceived.ToString())
Dim totalBytes As Double = Double.Parse(e.TotalBytesToReceive.ToString())
Dim percent As Double = bytesIn / totalBytes * 100
RaiseEvent UpdatePercent(Int32.Parse(Math.Truncate(percent).ToString()))
End Sub
Private Sub DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
If e.Error Is Nothing Then
RaiseEvent Status("Update Finished")
RaiseEvent DownloadComplete()
GenerateAndRunCMD()
Else
'TODO: Error while downloading your new .exe, do what you want (You can also raise DownloadComplete event and manage it)
End If
End Sub
#End Region
Private Sub GenerateAndRunCMD()
Dim sCMDcommands As New System.Text.StringBuilder
With sCMDcommands
.Append("/C """)
.Append("timeout /t 2 /nobreak > NUL && ")
.Append("del """ & _CurrentPath.Path & _CurrentPath.FileName & """ && ")
.Append("move /y """ & _TempPath.Path & _TempPath.FileName & """ """ & _CurrentPath.Path & """ && ")
.Append(" start """" """ & _CurrentPath.Path & _CurrentPath.FileName & """""")
End With
Dim p As Process = New Process()
Dim pi As ProcessStartInfo = New ProcessStartInfo()
pi.Arguments = sCMDcommands.ToString
pi.FileName = "cmd.exe"
pi.UseShellExecute = False
pi.CreateNoWindow = True
p.StartInfo = pi
p.Start()
Application.Exit()
End Sub
End Class
Try
AU = New AutoUpdateClass(URLversiontxt, URLUpdatedApplication, Application.ExecutablePath)
AU.Run()
Catch ex As Exception : End Try