Mar 26, 2012 11:54 AM
(This post was last modified: Mar 26, 2012 04:33 PM by Onin42.)
Auto Update
Hi, i made this a long time ago. So i though of sharing it, Since i never found it anywhere.
My webhost was SPAM who don't let you have .txt and .exe files.
So i was obligated to use .html (for the .txt and more used) and .zip(which i descompact later) files.
This snippet can Kill Process, Auto update, Check for Updates, Auto Delete, ... hidden.
I'm bad at explications. so just give a test to the code.
For use just add the module to your project.
cl_update.KillPro(My.Application.Info.Title)
cl_update.NewUpdateHtml() 'True or false for Show the MsgBoxs
Code:
Hi, i made this a long time ago. So i though of sharing it, Since i never found it anywhere.
My webhost was SPAM who don't let you have .txt and .exe files.
So i was obligated to use .html (for the .txt and more used) and .zip(which i descompact later) files.
This snippet can Kill Process, Auto update, Check for Updates, Auto Delete, ... hidden.
I'm bad at explications. so just give a test to the code.
For use just add the module to your project.
cl_update.KillPro(My.Application.Info.Title)
cl_update.NewUpdateHtml() 'True or false for Show the MsgBoxs
Code:
Code
Imports Shell32 'Add always reference for Microsoft Shell and Automation
Imports System.Net, System.IO
Public Module cl_update
Public Sub KillPro(ByRef strProcessToKill As String)
Dim proc() As Process = Process.GetProcesses
Dim proDate As Date = Process.GetCurrentProcess.StartTime
For i As Integer = 0 To proc.GetUpperBound(0)
If proc(i).ProcessName = strProcessToKill And proc(i).StartTime > proDate Then
Dim msgx As Object = MsgBox("Another " & My.Application.Info.Title & " is currently executed," & vbCrLf & "Do you want to stop it?", MsgBoxStyle.YesNo + MsgBoxStyle.Information, "AutoUpdate")
If msgx = vbYes Then
proc(i).Kill()
'Else : End
End If
End If
Next
End Sub
Public Sub NewUpdateHtml(Optional ByVal CheckUpdate As Boolean = False)
Try
If My.Computer.Network.IsAvailable Then
'see new version in html (Because of webhost000 who don't accept .txt files and .exe files)
Dim Link As String = "http://www.YourSite.com/Execution/" & My.Application.Info.Title & "/version.html"
Dim wRequest As WebRequest = WebRequest.Create(Link)
Dim wResult As WebResponse = wRequest.GetResponse()
Dim sReader As New StreamReader(wResult.GetResponseStream())
Dim SourceCode As String = sReader.ReadToEnd()
sReader.Close()
wResult.Close()
'remove all the data for see just the new version
SourceCode = Split(SourceCode, "<h1>")(1)
SourceCode = Split(SourceCode, "</h1>")(0)
SourceCode = SourceCode.Trim
'choice: update or not
If Not SourceCode.Contains(" yx") Then
If (SourceCode.Replace(" o", "") <= My.Application.Info.Version.ToString) Then
If CheckUpdate = True Then MsgBox("You have the latest version of the software.", MsgBoxStyle.Information, "AutoUpdate")
Else
Dim msgx As Object = MsgBox("An update is available, do you want to dowload it?", MsgBoxStyle.Information + MsgBoxStyle.YesNo, "Auto Update")
If msgx = vbYes Then
'go to update page
'System.Diagnostics.Process.Start("http://www.YourSite.com/Execution/" & My.Application.Info.Title & "/version.html")
'change name current software
If Not File.Exists(My.Application.Info.DirectoryPath & "\Old" & My.Application.Info.Title & ".exe") Then
File.Move(Application.ExecutablePath, My.Application.Info.DirectoryPath & "\Old" & My.Application.Info.Title & ".exe")
Else
File.Delete(My.Application.Info.DirectoryPath & "\Old" & My.Application.Info.Title & ".exe")
File.Move(Application.ExecutablePath, My.Application.Info.DirectoryPath & "\Old" & My.Application.Info.Title & ".exe")
End If
'download update zip (Because of webhost000 who don't accept .exe file)
If Not File.Exists(My.Application.Info.DirectoryPath & "\New " & My.Application.Info.Title & ".zip") Then
My.Computer.Network.DownloadFile("http://www.YourSite.com/Execution/" & My.Application.Info.Title & "/" & SourceCode.Replace(" o", "") & "/" & My.Application.Info.Title & ".zip", My.Application.Info.DirectoryPath & "\New " & My.Application.Info.Title & ".zip")
Else
File.Delete(My.Application.Info.DirectoryPath & "\New " & My.Application.Info.Title & ".zip")
My.Computer.Network.DownloadFile("http://www.YourSite.com/Execution/" & My.Application.Info.Title & "/" & SourceCode.Replace(" o", "") & "/" & My.Application.Info.Title & ".zip", My.Application.Info.DirectoryPath & "\New " & My.Application.Info.Title & ".zip")
End If
'decompact zip file (update)
DescompactZip(My.Application.Info.DirectoryPath & "\New " & My.Application.Info.Title & ".zip", My.Application.Info.DirectoryPath)
'delete zip file
My.Computer.FileSystem.DeleteFile(My.Application.Info.DirectoryPath & "\New " & My.Application.Info.Title & ".zip")
'create the bat for remove current software
AppendText(Environ("tmp") & "\Xupdate.bat", "echo off" & vbCrLf & ":Repeat " & vbCrLf & "del """ & My.Application.Info.DirectoryPath & "\Old" & My.Application.Info.Title & ".exe""" & vbCrLf & "if exist """ & My.Application.Info.DirectoryPath & "\Old" & My.Application.Info.Title & ".exe"" goto Repeat" & vbCrLf & "del """ & Environ("tmp") & "\Xupdate.bat""")
Interaction.Shell(Environ("tmp") & "\Xupdate.bat", AppWinStyle.Hide)
'run updated software
'Interaction.Shell(My.Application.Info.DirectoryPath & "\" & My.Application.Info.Title & ".exe", AppWinStyle.NormalFocus, False)
MsgBox("Updated Sucessfully", MsgBoxStyle.Information, "AutoUpdate")
Application.Exit()
Else
If SourceCode.Contains(" o") Then End
End If
End If
Else : AutoDelete()
End If
End If
Catch ex As Exception
MsgBox("Error come, Details:" & vbCrLf & ex.Message.ToString, MsgBoxStyle.Exclamation, "AutoUpdate")
Application.Exit()
End Try
End Sub
Private Sub AutoDelete()
If File.Exists(Environ("tmp") & "\Xdelete.bat") Then File.Delete(Environ("tmp") & "\Xdelete.bat")
AppendText(Environ("tmp") & "\Xdelete.bat", "echo off" & vbCrLf & ":Repeat " & vbCrLf & "del """ & Application.ExecutablePath & """" & vbCrLf & "if exist """ & Application.ExecutablePath & """ goto Repeat" & vbCrLf & "del """ & Environ("tmp") & "\Xdelete.bat""")
Interaction.Shell(Environ("tmp") & "\Xdelete.bat", AppWinStyle.Hide)
Application.Exit()
End Sub
Private Sub AppendText(ByVal vsFilePath As String, ByVal vsText As String)
With New System.IO.StreamWriter(vsFilePath, True)
.WriteLine(vsText)
.Close()
End With
End Sub
Private Sub DescompactZip(ByVal strSource As String, ByVal strDest As String)
Dim Sh As New Shell
Dim SF As Folder = Sh.NameSpace(strSource) 'Sh.NameSpace("C:\ZipFile.zip")
Dim DF As Folder = Sh.NameSpace(strDest) 'Sh.NameSpace("C:\Unzipped Files")
For Each F As FolderItem In SF.Items
DF.CopyHere(F)
Next
End Sub
End Module


![[Image: ouURNlq.png]](https://i.imgur.com/ouURNlq.png)

![[Image: logo.gif]](https://datap.pro/threadimage/logo.gif)