I am trying to use WebClient to download a .zip file from my server. In debugging mode the code works perfectly, but when I deploy my software the file does not even begin to download.

After my program checks to see if an update is available, it should download the .zip file from my web server via the following code:

Imports System
Imports System.Net
Imports System.Net.WebClient

Public WithEvents fileDownloader As Net.WebClient

Public gstrTempPath As String = ""

Private Sub FileDownload_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim gstrTempPath As String = Application.StartupPath & "\Temp"
        If Not Directory.Exists(gstrTempPath) Then Directory.CreateDirectory(gstrTempPath)

        InitiateDownload("http://www.mydomain.com/SETUP.zip", Application.StartupPath & "\Temp\SETUP.zip")

    End Sub

Private Sub InitiateDownload(ByVal remoteUrl As String, ByVal localFilePath As String)
        'Reset the progress indicators.
        Me.PercentLabel.Text = (0).ToString("p0")
        Me.ProgressBar1.Value = 0

        If Me.fileDownloader Is Nothing Then
            'This is the first download so create the web client.
            Me.fileDownloader = New Net.WebClient
        End If

        'Start downloading the file in the background.
        Me.fileDownloader.DownloadFileAsync(New Uri(remoteUrl), localFilePath)
    End Sub

Any help would be greatly appreciated as to why this file downloads perfectly when in debugging mode, but not when I deploy my program as a final copy. Perhaps I am missing a required .dll???

Recommended Answers

All 2 Replies

My first thought is that when you deploy the program, the CreateDirectory method may not have sufficient rights to create a folder on the hard drive, and thus skips the call to InitiateDownload.
Have you tried running it as Administrator?

My second thought is that you may wish to add a Try...Catch statement in both the FileDownload_Load event and in the InitiateDownload method.
That way you can track where things goes sideways, if that is the case.

Other than that, your code is spot on. :)

Thanks for your reply. I found the answer to my problem almost as soon as you responded to my post. I was running the program as Administrator which is fine. I also created the rights to the server directory as well. Both of which were great thoughts on your part.

My problem was that I had coded to unzip the program after it downloaded, but forgot to include an important .dll along with my project. I found that out by error trapping (also as you suggested). Thanks for all of your help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.