Advertisement
Mavamaarten

Auto-updater in VB.Net

Aug 12th, 2013
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.99 KB | None | 0 0
  1. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  2. MAIN FORM
  3. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  4.  
  5. Dim VERSION As New Version(1, 0, 6) 'This is the current version of the program. Change this when you release a new update.
  6.  
  7. Sub CheckForUpdates() 'Note: you can run this sub in a new thread, it's threadsafe.
  8.         Try
  9.             Dim VersionData As String = New Net.WebClient().DownloadString("http://dl.dropbox.com/u/1424119/VERSION_SCD") 'This will be the URL to your version file. More about this later.
  10.             Dim NewestVersion As New Version(Split(VersionData, "/_/_~_/_/")(0)) 'Get the newest version
  11.             Dim DownloadURL As String = Split(VersionData, "/_/_~_/_/")(1) 'Get the download URL of the newest version
  12.             Dim Changelog As String = Split(VersionData, "/_/_~_/_/")(2) 'Get the changelog
  13.             If NewestVersion > VERSION Then 'Compare the two versions
  14.                 Dim Result As DialogResult
  15.                 Me.Invoke(New MethodInvoker(Sub()
  16.                                                 Result = MessageBox.Show("A newer version of SoundCloud Desktop is available." & vbCr & "This update is recommended." & vbCr & vbCr & "Changelog:" & Changelog & vbCr & vbCr & "Perform update?", "SoundCloud Desktop Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
  17.                                             End Sub))
  18.                 If Result = Windows.Forms.DialogResult.Yes Then 'If you want to update, then show the update dialog
  19.                     Me.Invoke(New MethodInvoker(Sub()
  20.                                                     frmUpdate.URL = DownloadURL
  21.                                                     frmUpdate.Show()
  22.                                                     frmUpdate.Location = New Point(Me.Left + (frmUpdate.Width / 2), Me.Top + (frmUpdate.Height / 2)) 'Center the update form in the parent form (that looks better :D)
  23.                                                 End Sub))
  24.                 End If
  25.             End If
  26.         Catch : End Try
  27.     End Sub
  28.  
  29. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  30. UPDATE FORM (frmUpdate)
  31. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  32. Public Class frmUpdate
  33.       WithEvents WC As New Net.WebClient
  34.       Public URL As String
  35.  
  36.       Private Sub frmUpdate_Shown(sender As Object, e As EventArgs) Handles Me.Shown
  37.             WC.DownloadFileAsync(New Uri(URL), Application.StartupPath & "\update.exe") 'When the form is shown, download the file to update.exe in the executable's directory.
  38.             frmMain.Hide()
  39.       End Sub
  40.  
  41.       Private Sub WC_DownloadFileCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles WC.DownloadFileCompleted
  42.             SoundCloudProgressBar1.Value = 100
  43.  
  44.             Dim Batch As New System.Text.StringBuilder 'We create a new stringbuilder here and write a new batchfile
  45.             Batch.AppendLine("@echo off")
  46.             Batch.AppendLine("taskkill /IM " & FileIO.FileSystem.GetFileInfo(Application.ExecutablePath).Name & " /F")
  47.             Batch.AppendLine("ping localhost > nul")
  48.             Batch.AppendLine("del /f " & Chr(34) & Application.ExecutablePath & Chr(34))
  49.             Batch.AppendLine("ren " & Chr(34) & Application.StartupPath & "\update.exe" & Chr(34) & " " & FileIO.FileSystem.GetFileInfo(Application.ExecutablePath).Name)
  50.             Batch.AppendLine(Chr(34) & Application.ExecutablePath & Chr(34))
  51.             Batch.AppendLine("del %0")
  52.  
  53.             My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\update.bat", Batch.ToString, False, System.Text.Encoding.Default) 'We save the batch file...
  54.             Shell(Application.StartupPath & "\update.bat", AppWinStyle.Hide) 'and run it here :)
  55.       End Sub
  56.  
  57.       Private Sub WC_DownloadProgressChanged(sender As Object, e As Net.DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged
  58.             lblPercentage.Text = "Update progress: " & e.ProgressPercentage & "%" 'Show how far the download has progressed
  59.             SoundCloudProgressBar1.Value = e.ProgressPercentage 'And show it in a progressbar too
  60.       End Sub
  61. End Class
  62.  
  63. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  64. VERSION FILE
  65. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  66. 1.0.7/_/_~_/_/http://dl.dropbox.com/u/1424119/SoundCloud_Desktop.exe/_/_~_/_/
  67. [1.0.6] *Added volume slider
  68.         *Added "Copy URL" to context menu
  69.         *Internal optimizations
  70. [1.0.7] *Media hotkeys (play, pause, stop, next) added
  71.         *Many bugfixes
  72.         *Added ability to sort results
  73.         *Added refresh functionality
  74.         *Added sloth for increased awesomeness
  75.  
  76. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  77. EXPLANATION OF VERSION FILE
  78. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  79. Basically, /_/_~_/_/ is the delimiter. I know it looks a bit retarded but I felt like doing this :P
  80. The version file looks like this:
  81. <CURRENT VERSION> delimiter <DOWNLOAD URL> delimiter <CHANGELOG>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement