Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Download Manager for Application Updates

0.00/5 (No votes)
16 Jul 2007 1  
This article describes how to build a component for managing download files of a program.

Introduction

As a .NET developer, for implementing a project that will check on a web server for updates, I use Microsoft "ClickOnce". But because this application runs on Windows locked, I decided to develop one myself.

Background

The principal components of this project are:

  1. An XML file that resides on the web server
  2. Component Update
  3. Client for the component update
  4. The principal program

The XML file is structured this way:

<?xml version="1.0" encoding="utf-8" ?>
<Updates>
    <update id="1" dtUpdate="2007_06_01" version="1_0_0_1" RestartFastBet="False">
        <details>
            <file remote="http://youWebServer/fastbet_Download/1_0_0_1/fastbet.exe" 
                      locale="fastbet.exe" MustStart="False"></file>
            <config key="Last_Update" section="Update" value="2007_06_01"></config>
        </details>
    </update>
    <update id="2" dtUpdate="2007_07_02" version="1_0_0_1" RestartFastBet="False">
        <details>
            <config key="Last_Update" section="Update" value="2007_07_02"></config>
        </details>
    </update>
</Updates>

Every tag update is a new version of the program. The date of a version is in the attribute dtUpdate inside the update node, so if a version of a program (that I match with the key getSetting("FastBet", "Update", "LastUpdate")) is old, the first thig that I do is to download the file and metadata.

The file of the new version is found in the attributes inside the <file> node. The attribute remote is the URI where the file must be downloaded, and locale is the path where the file will be copied on the local machine. Metadata is represented with the node <config>. The metadata section has the same attributes used to save a key on the Registry:

Savesetting("Fastbet", "[section]","[key]", "[value]")

Below we can see how this works!

Using the code

In the component update part, I use the class Update. In this class, there is a method that will get the XML file and it will be loaded in the XMLDoc property:

Public Sub CheckForUpdate(ByVal sUrl As String) 
    Dim Request As HttpWebRequest = CType(WebRequest.Create(sUrl), HttpWebRequest) 
    Dim response As HttpWebResponse = CType(Request.GetResponse, HttpWebResponse) 
    Dim responseStream As StreamReader = New StreamReader

    response.GetResponseStream()
    Dim output As String = responseStream.ReadToEnd()
    responseStream.Close() 
    response.Close()
    xMLdOC.LoadXml(output) 
End Sub

Function Main()
    Try
        Dim bytesProcessed As Integer = 0
        Dim remoteStream As Stream = Nothing
        Dim localeStream As Stream = Nothing
        Dim response As HttpWebResponse = Nothing
        Dim request As HttpWebRequest = CType(WebRequest.Create(remoteFile), HttpWebRequest)
        response = request.GetResponse
        If Not response Is Nothing Then
            remoteStream = response.GetResponseStream
            localeStream = File.Create(LocaleFile)
            Dim buffer(1024) As Byte
            Dim Bytesread As Integer
            Do
                Bytesread = remoteStream.Read(buffer, 0, buffer.Length)
                localeStream.Write(buffer, 0, Bytesread)
                bytesProcessed += Bytesread
                If Bytesread = 0 Then Exit Do
            Loop
            If Not response Is Nothing Then response.Close()
            If Not remoteStream Is Nothing Then remoteStream.Close()
            If Not localeStream Is Nothing Then localeStream.Close()
        End If
        Return bytesProcessed

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here