Some people might find this usefull as much as i have so here it is. This code downloads a list of files from a txt file to the directory you set it to. Just add a button and a progressbar (change its name to Progbar) and your good to go, enjoy
vb.net Code:
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
"Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz
Note: Currently as the code is structured, you MUST have all the files on the same server.
As for your list, it was structured like this when the code was written so that when the lines are merged and called to be downloaded there would be no confusion.
Tweaker99:- It downloads anything you want assuming you have the Location of the file on the web...
michalss:- Simply add a line just before 'Do While k > 0' like below
Code:
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)
TextBox1.Text = TextBox1.Text & vbNewLine & "-> Downloading " & line '<<< Line is the File, This will add a new line saying what it is downloading each time the file changes
TextBox1.Refresh() '<<< Refresh the Textbox so it shows it
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
Now for my Question How can i calculate the Transfer Rate from this?
Sorry for the late reply. I've been really inactive as ive been tied up in a big project the past months. Heres a link to where it shows you can add the download speed (im assuming thats what you mean by transfer rate?) http://www.codeproject.com/KB/vb/dow...ogressbar.aspx
"Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz