Imports spazio
Public Class frmDownload
#Region "Strutture"
Public Structure DownloadList
Dim URL, path As String
End Structure
#End Region
#Region "Variabili"
Public DList(19) As DownloadList
Public TCP As New System.Net.WebClient
Public SHour, SMinute, SSecond As Int16
#End Region
#Region "Metodi personalizzati"
Public Function HourDifference(ByVal Hour As Byte, ByVal Minutes As Byte, ByVal Seconds As Byte) As String
Dim NHour, NMinute, NSecond As Byte
Dim Now As New spazio.GestioneData
NHour = Now.ora
NMinute = Now.minuti
NSecond = Now.secondi
If NHour >= Hour Then
NHour -= Hour
Else
NHour = (24 - Hour) + NHour
End If
If NMinute >= Minutes Then
NMinute -= Minutes
Else
NMinute = (60 - Minutes) + NMinute
End If
If NSecond >= Seconds Then
NSecond -= Seconds
Else
NSecond = (60 - Seconds) + NSecond
End If
Return NHour & ":" & NMinute & ":" & NSecond
End Function
Public Sub EndOfDownload()
lblPercentage.Text = "0 bytes scaricati su 0, completato 0%"
lblTime.Text = "Iniziato alle ore:"
lblElapsed.Text = "Tempo rimasto:"
lblName.Text = "Nome del file:"
SHour = 0
SMinute = 0
SSecond = 0
End Sub
Public Sub NewDownload(ByVal filename As String)
Dim Now As New spazio.GestioneData
SHour = Now.ora
SMinute = Now.minuti
SSecond = Now.secondi
If filename.Length > 90 Then
filename = filename.Remove(89, filename.Length - 90)
filename = filename + "..."
End If
lblPercentage.Text = "0 bytes scaricati su 0, completato 0%"
lblTime.Text = "Iniziato alle ore: " & SHour & ":" & SMinute & ":" & SSecond
lblElapsed.Text = "Tempo trascorso: "
lblName.Text = "Nome del file: " + filename
End Sub
Private Sub PrgChngd(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
prgBar.Value = e.ProgressPercentage
Me.Text = "Global Web - " & e.ProgressPercentage & "% download"
lblPercentage.Text = e.BytesReceived & " bytes scaricati su " & e.TotalBytesToReceive & ", completato " & e.ProgressPercentage & "%"
lblElapsed.Text = "Tempo trascorso: " + HourDifference(SHour, SMinute, SSecond)
End Sub
Private Sub PrgComplete(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
If e.Cancelled = True Then
MsgBox("Download del dile " & DList(0).URL & " cancellato!", MsgBoxStyle.Information, "Download cancellato")
Else
MsgBox("Download del file " & DList(0).URL & " completato!", MsgBoxStyle.Information, "Download completato")
End If
'Riordinamento array:
For i As Int16 = 1 To 19
DList(i - 1) = DList(i)
Next
DList(19) = Nothing
lstDown.Items.RemoveAt(0)
If lstDown.Items.Count > 0 Then
lstDown.SelectedIndex = 0
TCP.DownloadFileAsync(New Uri(DList(0).URL), DList(0).path)
NewDownload(DList(0).URL)
Else
MsgBox("Tutti i downloads sono stati completati!", MsgBoxStyle.Information, "Downloads completati")
End If
prgBar.Value = 0
EndOfDownload()
End Sub
Public Function GetExtension(ByVal name As String) As String
Dim s As String = ""
Dim i, a As Int16
For i = 0 To name.Length - 1
If name.Chars(i) = "." Then
a = i + 1
End If
Next
For i = a To name.Length - 1
s = s & name.Chars(i)
Next
GetExtension = s
End Function
Public Function GetName(ByVal name As String) As String
Dim s As String = ""
Dim i, LastBS, LastDot As Int16
For i = 0 To name.Length - 1
If name.Chars(i) = "\" Then
LastBS = i + 1
End If
If name.Chars(i) = "." Then
LastDot = i
End If
Next
For i = LastBS To LastDot - 1
s = s & name.Chars(i)
Next
GetName = s
End Function
#End Region
#Region "Eventi della windows form"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler TCP.DownloadProgressChanged, AddressOf PrgChngd
AddHandler TCP.DownloadFileCompleted, AddressOf PrgComplete
End Sub
Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
If lstDown.Items.Count > 0 Then
TCP.DownloadFileAsync(New Uri(DList(0).URL), DList(0).path)
NewDownload(DList(0).URL)
Else
MsgBox("Nessun elemento da scaricare è presente nella lista!", MsgBoxStyle.Exclamation, "Errore")
End If
End Sub
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
If txtURL.Text = "" Or txtFile.Text = "" Then
MsgBox("Inserire prima l'indirizzo da cui scaricare il file e la locazione in cui copiarlo!", MsgBoxStyle.Exclamation, "Errore")
Exit Sub
End If
lstDown.Items.Add(txtURL.Text & " da scaricare in " & txtFile.Text)
DList(lstDown.Items.Count - 1).path = txtFile.Text
DList(lstDown.Items.Count - 1).URL = txtURL.Text
txtURL.Text = ""
txtFile.Text = ""
End Sub
Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click
If lstDown.SelectedIndex > 0 Then
lstDown.Items.RemoveAt(lstDown.SelectedIndex)
End If
If lstDown.SelectedIndex = 0 Then
If MsgBox("Il file è attualemente in download. Bloccarlo?", MsgBoxStyle.Question + vbYesNo, "Download") = MsgBoxResult.Yes Then
TCP.CancelAsync()
lstDown.Items.RemoveAt(0)
EndOfDownload()
End If
End If
End Sub
Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
TCP.CancelAsync()
EndOfDownload()
End Sub
Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowse.Click
Dim DefaultExt As String = GetExtension(txtURL.Text)
FOpen.Filter = "File " & DefaultExt & "|*." & DefaultExt & "|Tutti i file|*.*"
If FOpen.ShowDialog = Windows.Forms.DialogResult.OK Then
txtFile.Text = FOpen.FileName
End If
End Sub
Private Sub cmdStopAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStopAll.Click
lstDown.Items.Clear()
TCP.CancelAsync()
EndOfDownload()
End Sub
Private Sub cmdInternet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInternet.Click
Me.Close()
End Sub
#End Region
End Class