-1

I want to be able to add multiple download links and for them to go into a single folder which is selected by the user in a Folder Browser Dialog

The code you see below works great except only for a single file. I have tried changing all 'savefiledialog1' to 'folderbrowserdialog1' instead. However this leads to me clicking download and nothing happening even if only a single link is entered.

 Private Sub BtnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
        If (SaveFileDialog1.ShowDialog() = DialogResult.OK) Then
            txtSave1.Text = SaveFileDialog1.FileName
            btnDownload.Enabled = True
        End If
    End Sub


    ' ------------ DOWNLOADING SECTION ------------
    Private WithEvents HTTPCLIENT As WebClient
    Private Sub BtnDownload_Click(sender As Object, e As EventArgs) Handles 
        btnDownload.Click
        btnDownload.Enabled = False
        txtSave1.Enabled = False
        btnBrowse.Enabled = False
        btnDownload.Enabled = False

        HTTPCLIENT = New WebClient
        Dim Download As String
            Download = Links(i)
            Dim User = Environment.UserName
            Dim Save As String = txtSave1.Text
            Try
                HTTPCLIENT.DownloadFileAsync(New Uri(Download), Save)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    End Sub

I expected the folder browser dialog to just be a general save path where the file being downloaded is placed into that folder, however I am given an error. The code above works but only for a single file.

I have code that can retrieve the download name and extension which I plan to add to the path once i figure this part out.

Joel
  • 13
  • 4

1 Answers1

0

You can use the FolderBrowserDialog. Once you get the path, you combine it with each filename you will download. Use System.IO.Path.Combine()

Private Sub BtnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
    Using fbd As New FolderBrowserDialog()
        If fbd.ShowDialog() = DialogResult.OK Then
            txtSave1.Text = fbd.SelectedPath
            btnDownload.Enabled = True
        End If
    End Using
End Sub

Private Sub BtnDownload_Click(sender As Object, e As EventArgs) Handles btnDownload.Click
    Try
        btnDownload.Enabled = False
        txtSave1.Enabled = False
        btnBrowse.Enabled = False
        btnDownload.Enabled = False
        Dim exceptionMessages As New List(Of String)
        Using client = New WebClient()
            ' configure client here as needed i.e. add Credentials
            For Each link In Links
                Try
                    client.DownloadFileAsync(New Uri(link), Path.Combine(txtSave1.Text, link))
                Catch ex As Exception
                    exceptionMessages.Add(ex.Message)
                End Try
            Next
        End Using
        If exceptionMessages.Any() Then MessageBox.Show($"Exception{If(exceptionMessages.Count > 1, "s", "")}: {String.Join(Environment.NewLine, exceptionMessages)}")
    Finally
        txtSave1.Enabled = True
        btnBrowse.Enabled = True
        btnDownload.Enabled = True
    End Try
End Sub

Note that I will not post an answer with IDisposable objects without Using (in most cases) so FolderBrowserDialog and WebClient are both in Usings. You may need to add additional configuration to the WebClient before downloading.

Also, you probably don't want a separate message for each Exception, if any. So the messages can be cached and shown all at once.

I inserted a Finally for you to set control states back to default when done. This is up to you.

Lastly, the work is being done on a UI thread as evidenced by it being one inside the button click handler. You should move it off the UI even though you aren't blocking. This is outside the scope of the question.

djv
  • 15,168
  • 7
  • 48
  • 72