Imports System.IO
Imports System.Net
Imports Microsoft.VisualBasic.FileIO.FileSystem
Public Class Form1
Private Const sfilename As String = "c:\Users\****\Desktop\list_of_files.txt"
Private Const url As String = "http://www.***.com/files"
Private Const spath As String = "c:\Users\****\Desktop\test"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'CHECK FOR LIST
If Not My.Computer.FileSystem.FileExists(sfilename) Then
MsgBox("No list found, exiting application", MsgBoxStyle.Critical)
Application.Exit()
End If
Dim sr As New IO.StreamReader(sfilename)
Dim line As String = sr.ReadLine()
Dim req As Net.WebRequest
Dim resp As IO.Stream
Dim out As IO.BinaryWriter
Do While line IsNot Nothing
req = Net.HttpWebRequest.Create(url & line)
Dim progmax As Net.HttpWebResponse = req.GetResponse()
resp = req.GetResponse().GetResponseStream()
'===========================
'CREATES DIRECTORY IF NEEDED BEFORE IT CALLS TO OPEN A NEW FILESTREAM AND THERE IS NONE
'===========================
Dim filepath As String = spath & line
If Not Directory.Exists(filepath.Replace(Path.GetFileName(filepath), String.Empty)) Then Directory.CreateDirectory(filepath.Replace(Path.GetFileName(filepath), String.Empty))
'===========================
out = New IO.BinaryWriter(New IO.FileStream(spath & line, IO.FileMode.OpenOrCreate))
Dim buf(4096) As Byte
Dim k As Int32 = resp.Read(buf, 0, 4096)
Do While k > 0
out.Write(buf, 0, k)
k = resp.Read(buf, 0, 4096)
'===================
'READ FROM THE BUFFER AND REPORT PROGRESS TO THE PROGBAR
'===================
Progbar.Maximum = progmax.ContentLength
If Progbar.Value + k <= Progbar.Maximum Then
Progbar.Value += k
Else
Progbar.Value = Progbar.Maximum
End If
Loop
resp.Close()
out.Close()
line = sr.ReadLine()
Loop
End Sub
End Class