Imports System.Runtime.InteropServices
Imports System.Text
#Region " VARIABLES "
Dim downloadedINIpath As String = "http://www.example.com/version.ini"
Dim fullpath1 As String = IO.Path.Combine(FileIO.SpecialDirectories.CurrentUserApplicationData, "settings.ini") 'Path of local .INI file
Dim fullpath2 As String = IO.Path.Combine(FileIO.SpecialDirectories.CurrentUserApplicationData, "version.ini") 'Path of downloaded .INI file
Dim newUpdaterPath As String = IO.Path.Combine(FileIO.SpecialDirectories.CurrentUserApplicationData, "Updater.exe") 'Path of Updater.exe
Dim Version As String = My.Application.Info.Version.ToString 'DONT EDIT
#End Region
#Region " ON FORM CLOSING "
Dim fullpath3 As String = IO.Path.Combine(FileIO.SpecialDirectories.CurrentUserApplicationData, "version.ini") 'Updates Path of downloaded .INI file
Dim fileExists As Boolean = My.Computer.FileSystem.FileExists(fullpath3)
If fileExists = True Then
My.Computer.FileSystem.DeleteFile(fullpath3, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently) 'Clean up version.ini when app is shutdown
End If
#End Region
#Region " ON FORM LOAD "
If My.Computer.FileSystem.FileExists(fullpath1) = False Then
INIReadWrite.WriteINI(fullpath1, "settings", "version", Version) 'Write new local .INI file if it doesn't exist
Else
If INIReadWrite.ReadINI(fullpath1, "settings", "version") <> Version Then
INIReadWrite.WriteINI(fullpath1, "settings", "version", Version)
End If
End If
If My.Computer.FileSystem.FileExists(IO.Path.Combine(FileIO.SpecialDirectories.CurrentUserApplicationData, "version.ini")) Then
My.Computer.FileSystem.DeleteFile(IO.Path.Combine(FileIO.SpecialDirectories.CurrentUserApplicationData, "version.ini"), FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently) 'If previous downloaded .INI file still exists then delete it
End If
My.Computer.Network.DownloadFile(downloadedINIpath, fullpath2) 'Download new .INI file
If INIReadWrite.ReadINI(fullpath2, "settings", "version") <> INIReadWrite.ReadINI(fullpath1, "settings", "version") Then 'Check the downloaded .INI file version against the local .INI files version
If MsgBox("A new update is available, do you want to update now?", MsgBoxStyle.YesNo, "New update") = MsgBoxResult.Yes Then 'If new update found, ask if they want to update now
Try
Dim a1 As String = """http://www.example.com/myapp(.zip/.exe)""" 'Address of new program to download(Change this to yours(can be either .zip or .exe file extensions))
Dim a2 As String = """" & Application.StartupPath & """" 'Download it to the applications folder
Dim a3 As String = """" & My.Application.Info.AssemblyName & """" 'Gives updater the applications name
Dim a4 As String = a1 & " " & a2 & " " & a3
Process.Start(newUpdaterPath, a4) 'Start the updater
Catch ex As Exception
Application.Exit() 'If cannot start for some reason, exit the program.
End Try
End If
Else
End If
#End Region
Class INIReadWrite
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function WritePrivateProfileString(ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Boolean
End Function
Public Shared Function ReadINI(ByVal File As String, ByVal Section As String, ByVal Key As String) As String
Dim sb As New StringBuilder(500)
GetPrivateProfileString(Section, Key, "", sb, sb.Capacity, File)
Return sb.ToString
End Function
Public Shared Sub WriteINI(ByVal File As String, ByVal Section As String, ByVal Key As String, ByVal Value As String)
WritePrivateProfileString(Section, Key, Value, File)
End Sub
End Class