Page 1 of 1

Check and download updates from web. Rate Topic: ***** 1 Votes

#1 TEH  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 151
  • Joined: 30-January 08

Posted 02 February 2010 - 06:11 AM

Here has been lot of programmers asking about auto update in VB.NET –forum.
So here you go:

I'm using files which I get when I build my Setup-project, so I think this is not working in Visual Studio Express-editions.

First things first, I made this as a class so I can use it in different projects, without rewriting it, with just a little modifications.
Class with functionality is named as Version.vb and it contains:

Version.vb

Imports System.IO
Imports System.Net
Imports System.Text
Class Web_update
    Dim version As String
    Public Shared Downuri As String
    Public Shared Sub Main()
        Dim URI As String
        '
        'REPLACE Program WITH YOUR APPLICATIONS NAME.
        '
        'REMEMBER TO EDIT FILEVERSION IN ASSEMBLY INFORMATION
        '
        '
        'Edit URI to your version.html-file. 
        URI = "http://your.domain.com/Program_version.html"
        'Edit URI to your programs zip-file
        Downuri = "http://your.domain.com/Program.zip"

        Dim wr As HttpWebRequest = CType(WebRequest.Create(URI.ToString), HttpWebRequest)
        Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
        Dim str As Stream = ws.GetResponseStream()
        Dim inBuf(100000) As Byte
        Dim bytesToRead As Integer = CInt(inBuf.Length)
        Dim bytesRead As Integer = 0
        While bytesToRead > 0
            Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
            If n = 0 Then
                Exit While
            End If
            bytesRead += n
            bytesToRead -= n
        End While
        Dim fstr As New FileStream("version.txt", FileMode.OpenOrCreate, FileAccess.Write)
        fstr.Write(inBuf, 0, bytesRead)
        str.Close()
        fstr.Close()
        Dim sr As StreamReader = New System.IO.StreamReader("version.txt")
        Dim version As Integer = CInt(sr.ReadToEnd.Replace(".", "").Substring(0, 4))
        sr.Close()
        If version > CInt(Application.ProductVersion.Replace(".", "")) Then
            Dialog1.ShowDialog()
        Else
            MessageBox.Show("There is no new updates.", "Update", MessageBoxButtons.OK)
        End If
    End Sub 'Main
End Class 'Web_update
 


As you can see from the code below I have created checking in the dialog which contains 3 buttons.
One for downloading the zip-file from web, one for downloading and running the update, and Cancel button.

Dialog1.vb

'REPLACE Program WITH YOUR APPLICATIONS NAME.

Imports System.Windows.Forms

Public Class Dialog1
    Dim Client As New Net.WebClient()

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
    
    'Download file from web. 	
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DR As DialogResult = FolderBrowserDialog1.ShowDialog
        If DR = Windows.Forms.DialogResult.OK Then
            Client.DownloadFile(Web_update.Downuri, _
            FolderBrowserDialog1.SelectedPath.ToString & _
            "\Program_Update_" & Date.Today.ToShortDateString.ToString & ".zip")
        End If
        Me.Close()
    End Sub

    'Download file from web to Temp-directory and run.
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Client.DownloadFile(Web_update.Downuri, _
        My.Computer.FileSystem.SpecialDirectories.Temp.ToString & "\Program_Update.zip")
        Process.Start(My.Computer.FileSystem.SpecialDirectories.Temp.ToString & _
        "\Program_Update.zip")
        Application.Exit()    
    End Sub
End Class



In Form1 I have StatusStrip1 which contains ToolStripSplitButton1 and this contains Check For Updates-ToolStripMenuItem
Form1.vb
 Private Sub CheckForUpdatesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckForUpdatesToolStripMenuItem.Click
        Web_update.Main()
    End Sub
End Class




version.html
-file contains only the number of current version for example
1005

and nothing else, so there is no dots between numbers.

Program.Zip-file contains 2 files which contais Setup.exe and Program_Setup.msi whitch you get when you build your Setup-project
You need to compress these files manually.

I hope this helps someone to get Updating of program working.
Attached Image

I added the Version.zip-file witch contains my version.vb, Dialog1.vb, Dialog1.resx and Dialog1.Designer.vb-files

Attached File(s)


This post has been edited by TEH: 04 February 2010 - 02:34 AM


Is This A Good Question/Topic? 3
  • +

Replies To: Check and download updates from web.

#2 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Posted 12 February 2010 - 09:01 AM

Cool thanks.
Was This Post Helpful? 0
  • +
  • -

#3 hackerxmen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-April 10

Posted 13 April 2010 - 02:21 AM

thanks (~|~) :smartass:
Was This Post Helpful? 0
  • +
  • -

#4 PrivT  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 16-February 09

Posted 18 April 2010 - 08:19 PM

Thanks a lot man this really helped :].

-Jd
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Martin*


Reputation:

Posted 25 August 2010 - 08:22 AM

The only problem with this script is that it doesnt check if the user is connected to the internet. If the user is not connected this script will cause your app to freeze/hang and eventually crash. It really needs a try / catch exception method added to make it more stable :sailor:
Was This Post Helpful? 0

#6 TEH  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 151
  • Joined: 30-January 08

Posted 03 September 2010 - 01:05 AM

you can use for example this snippet from PsychoCoder to check if internet connection is available:Check for internet connection
Was This Post Helpful? 0
  • +
  • -

#7 Imba.Violet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-April 13

Posted 20 April 2013 - 07:03 AM

I don't know why but i can't use this ? , can someone help me

Error at this row
version As Integer = CInt(sr.ReadToEnd.Replace(".", "").Substring(0, 4))



Index and length must refer to a location within the string.

:(
Was This Post Helpful? 0
  • +
  • -

#8 TEH  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 151
  • Joined: 30-January 08

Posted 03 May 2013 - 04:59 AM

I think you have already figured this out, but do you have version.txt-file in your program startup folder? And you have rights to read it from there?
I have made that with windows XP so there can be case that you just dont have rights to write or read to your programs folder and you should be using appdata folder with win 7. If thats the case just replace all
"Version.txt"
filenames with
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData).ToString & "/Version.txt"

Was This Post Helpful? 1
  • +
  • -

#9 middlerig  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-September 13

Posted 19 September 2013 - 06:26 AM

View PostTEH, on 03 May 2013 - 04:59 AM, said:

I think you have already figured this out, but do you have version.txt-file in your program startup folder? And you have rights to read it from there?
I have made that with windows XP so there can be case that you just dont have rights to write or read to your programs folder and you should be using appdata folder with win 7. If thats the case just replace all
"Version.txt"
filenames with
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData).ToString & "/Version.txt"


Great article and I was able to make it work by changing to:
Thread.CurrentThread.CurrentCulture = New CultureInfo("nl-NL")
Client.DownloadFile(Web_update.Downuri, FolderBrowserDialog1.SelectedPath.ToString & "\XXX" & Date.Today.ToShortDateString.ToString & ".zip")
Thread.CurrentThread.CurrentCulture = originalCulture

otherwise, with Scottish localisation, the date was dd/mm/yy and all the "/" were an issue.

However the "process.start" does not start anything, and double clicking on the zip file generates an error in that the current version has to be removed using the 'control panel/ remove' process first. Is there a way to automate this?, kind regards
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1