Click here to Skip to main content
15,879,326 members
Articles / Web Development / ASP.NET

Download Manager for Application Updates

Rate me:
Please Sign up or sign in to vote.
2.45/5 (9 votes)
16 Jul 2007CPOL1 min read 39.1K   1.7K   35   3
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
<?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:

VB
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:

VB
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Italy Italy
I have played various roles in IT since around 1998 when I began writing microsoft Access forms. I then moved to mainly coding in Visual Basic around 1999. Since the release of .net I have been mainly working in .net focusing in vb.net and very massive use of SQL Server.
Now i working for italian bookmaker like Web Developer on distribuited applicaitons.

Comments and Discussions

 
GeneralCode Pin
vunhuthang5-Dec-07 20:35
vunhuthang5-Dec-07 20:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.