SHARE
TWEET

Portable Click-Once Implementation

maatspencer May 15th, 2017 (edited) 1,082 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System
  2. Imports System.Net
  3. Imports System.IO
  4. Imports System.Text.RegularExpressions
  5. Imports System.IO.Compression
  6. Imports System.IO.Compression.ZipFileExtensions
  7. Imports IWshRuntimeLibrary
  8. '
  9. Public Partial Class MainForm
  10.     Public Sub New()
  11.         ' The Me.InitializeComponent call is required for Windows Forms designer support.
  12.         Me.InitializeComponent()
  13.        
  14.         'Download Version.txt from github
  15.         Dim web_client As WebClient = New WebClient
  16.         Dim hostedVersion As String = web_client.DownloadString("VERSION URL")
  17.        
  18.        
  19.         '  Initial download
  20.         If Not My.Computer.FileSystem.FileExists(My.Application.Info.DirectoryPath & "\Version.txt") Then
  21.            
  22.             MessageBox.Show("Please wait while the application installs!")
  23.            
  24.             ' Download the version string
  25.             Using sw As StreamWriter = New StreamWriter(My.Application.Info.DirectoryPath & "\Version.txt")
  26.             sw.Write(hostedVersion.Trim)
  27.             End Using
  28.            
  29.             ' Download the package
  30.             downloadZip()
  31.            
  32.             ' Extract the Package
  33.             extractZip()
  34.            
  35.             ' Create a desktop Icon?
  36.             If MessageBox.Show("Would you like to create a desktop icon?","Update?",MessageBoxButtons.YesNo) = DialogResult.Yes Then
  37.                 Dim WshShell As WshShellClass = New WshShellClass
  38.                 Dim MyShortcut As IWshRuntimeLibrary.IWshShortcut
  39.                 ' The shortcut will be created on the desktop
  40.                 Dim DesktopFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
  41.                 MyShortcut = CType(WshShell.CreateShortcut(DesktopFolder & "\PROGRAM NAME.lnk"), IWshRuntimeLibrary.IWshShortcut)
  42.                 MyShortcut.TargetPath = Application.StartupPath & "\YourApp.exe" 'Specify target app full path
  43.                 MyShortcut.Save()
  44.             End If 
  45.            
  46.             'Start the Package
  47.             Process.Start(My.Application.Info.DirectoryPath & "\APP FOLDER\YourApp.exe")
  48.            
  49.         Else
  50.             ' Read Current Version
  51.             Dim version As String = ""
  52.             Using sr As StreamReader = New StreamReader(My.Application.Info.DirectoryPath & "\Version.txt")
  53.                 version = sr.ReadLine()
  54.             End Using
  55.            
  56.             If version.Trim() = hostedVersion.Trim Then
  57.                 ' Up to date
  58.                 Process.Start(My.Application.Info.DirectoryPath & "\APP FOLDER\YourApp.exe")
  59.             Else
  60.                 ' Out of date
  61.                 If MessageBox.Show("Your Draft Loadout is out of date do you want to Update it?","Update?",MessageBoxButtons.YesNo) = DialogResult.Yes Then
  62.                    
  63.                     ' Download the version string
  64.                     Using sw As StreamWriter = New StreamWriter(My.Application.Info.DirectoryPath & "\Version.txt")
  65.                         sw.WriteLine(hostedVersion.Trim)
  66.                     End Using
  67.                    
  68.                     ' Download the package
  69.                     downloadZip()
  70.                    
  71.                     ' Extract the Package
  72.                     extractZip()
  73.                    
  74.                     MessageBox.Show("Update Finished.")
  75.                     Process.Start(My.Application.Info.DirectoryPath & "\APP FOLDER\YourApp.exe")
  76.                 Else
  77.                     'Start the Package
  78.                     Process.Start(My.Application.Info.DirectoryPath & "\APP FOLDER\YourApp.exe")   
  79.                 End If
  80.            
  81.             End If
  82.         End If
  83.                
  84.     End Sub
  85.    
  86.     Private Shared Sub downloadZip()
  87.         Dim web_client As WebClient = New WebClient
  88.         web_client.DownloadFile("ZIPPED ARCHIVE URL",My.Application.Info.DirectoryPath & "\YourApp.zip")
  89.     End Sub
  90.    
  91.     Private Shared Sub extractZip()
  92.         Dim zipPath As String = My.Application.Info.DirectoryPath & "\YourApp.zip"
  93.         Dim extractPath As String = My.Application.Info.DirectoryPath & "\APP FOLDER"
  94.  
  95.         Using archive As ZipArchive = ZipFile.OpenRead(zipPath)
  96.             For Each entry As ZipArchiveEntry In archive.Entries
  97.                 Dim entryFullname = Path.Combine(extractPath, entry.FullName)
  98.                 Dim entryPath = Path.GetDirectoryName(entryFullName)
  99.                 If (Not (Directory.Exists(entryPath))) Then
  100.                     Directory.CreateDirectory(entryPath)
  101.                 End If
  102.  
  103.                 Dim entryFn = Path.GetFileName(entryFullname)
  104.                 If (Not String.IsNullOrEmpty(entryFn)) Then
  105.                     entry.ExtractToFile(entryFullname, True)
  106.                 End If
  107.             Next
  108.         End Using
  109.        
  110.         My.Computer.FileSystem.DeleteFile(My.Application.Info.DirectoryPath & "\YourApp.zip")
  111.     End Sub
  112.    
  113.     Sub MainFormShown(sender As Object, e As EventArgs)
  114.         Application.Exit
  115.             END
  116.     End Sub
  117. End Class
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top