Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Global Web 2.2 - Download.vb

Download.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports spazio
  2. Public Class frmDownload
  3. #Region "Strutture"
  4.     Public Structure DownloadList
  5.         Dim URL, path As String
  6.     End Structure
  7. #End Region
  8. #Region "Variabili"
  9.     Public DList(19) As DownloadList
  10.     Public TCP As New System.Net.WebClient
  11.     Public SHour, SMinute, SSecond As Int16
  12. #End Region
  13. #Region "Metodi personalizzati"
  14.     Public Function HourDifference(ByVal Hour As Byte, ByVal Minutes As Byte, ByVal Seconds As Byte) As String
  15.         Dim NHour, NMinute, NSecond As Byte
  16.         Dim Now As New spazio.GestioneData
  17.  
  18.         NHour = Now.ora
  19.         NMinute = Now.minuti
  20.         NSecond = Now.secondi
  21.         If NHour >= Hour Then
  22.             NHour -= Hour
  23.         Else
  24.             NHour = (24 - Hour) + NHour
  25.         End If
  26.         If NMinute >= Minutes Then
  27.             NMinute -= Minutes
  28.         Else
  29.             NMinute = (60 - Minutes) + NMinute
  30.         End If
  31.         If NSecond >= Seconds Then
  32.             NSecond -= Seconds
  33.         Else
  34.             NSecond = (60 - Seconds) + NSecond
  35.         End If
  36.  
  37.         Return NHour & ":" & NMinute & ":" & NSecond
  38.     End Function
  39.     Public Sub EndOfDownload()
  40.         lblPercentage.Text = "0 bytes scaricati su 0, completato 0%"
  41.         lblTime.Text = "Iniziato alle ore:"
  42.         lblElapsed.Text = "Tempo rimasto:"
  43.         lblName.Text = "Nome del file:"
  44.         SHour = 0
  45.         SMinute = 0
  46.         SSecond = 0
  47.     End Sub
  48.     Public Sub NewDownload(ByVal filename As String)
  49.         Dim Now As New spazio.GestioneData
  50.         SHour = Now.ora
  51.         SMinute = Now.minuti
  52.         SSecond = Now.secondi
  53.  
  54.         If filename.Length > 90 Then
  55.             filename = filename.Remove(89, filename.Length - 90)
  56.             filename = filename + "..."
  57.         End If
  58.  
  59.         lblPercentage.Text = "0 bytes scaricati su 0, completato 0%"
  60.         lblTime.Text = "Iniziato alle ore: " & SHour & ":" & SMinute & ":" & SSecond
  61.         lblElapsed.Text = "Tempo trascorso: "
  62.         lblName.Text = "Nome del file: " + filename
  63.     End Sub
  64.     Private Sub PrgChngd(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
  65.         prgBar.Value = e.ProgressPercentage
  66.         Me.Text = "Global Web - " & e.ProgressPercentage & "% download"
  67.         lblPercentage.Text = e.BytesReceived & " bytes scaricati su " & e.TotalBytesToReceive & ", completato " & e.ProgressPercentage & "%"
  68.         lblElapsed.Text = "Tempo trascorso: " + HourDifference(SHour, SMinute, SSecond)
  69.     End Sub
  70.     Private Sub PrgComplete(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
  71.         If e.Cancelled = True Then
  72.             MsgBox("Download del dile " & DList(0).URL & " cancellato!", MsgBoxStyle.Information, "Download cancellato")
  73.         Else
  74.             MsgBox("Download del file " & DList(0).URL & " completato!", MsgBoxStyle.Information, "Download completato")
  75.         End If
  76.  
  77.         'Riordinamento array:
  78.         For i As Int16 = 1 To 19
  79.             DList(i - 1) = DList(i)
  80.         Next
  81.         DList(19) = Nothing
  82.         lstDown.Items.RemoveAt(0)
  83.         If lstDown.Items.Count > 0 Then
  84.             lstDown.SelectedIndex = 0
  85.             TCP.DownloadFileAsync(New Uri(DList(0).URL), DList(0).path)
  86.             NewDownload(DList(0).URL)
  87.         Else
  88.             MsgBox("Tutti i downloads sono stati completati!", MsgBoxStyle.Information, "Downloads completati")
  89.         End If
  90.         prgBar.Value = 0
  91.         EndOfDownload()
  92.     End Sub
  93.     Public Function GetExtension(ByVal name As String) As String
  94.         Dim s As String = ""
  95.         Dim i, a As Int16
  96.  
  97.         For i = 0 To name.Length - 1
  98.             If name.Chars(i) = "." Then
  99.                 a = i + 1
  100.             End If
  101.         Next
  102.         For i = a To name.Length - 1
  103.             s = s & name.Chars(i)
  104.         Next
  105.         GetExtension = s
  106.     End Function
  107.     Public Function GetName(ByVal name As String) As String
  108.         Dim s As String = ""
  109.         Dim i, LastBS, LastDot As Int16
  110.  
  111.         For i = 0 To name.Length - 1
  112.             If name.Chars(i) = "\" Then
  113.                 LastBS = i + 1
  114.             End If
  115.             If name.Chars(i) = "." Then
  116.                 LastDot = i
  117.             End If
  118.         Next
  119.         For i = LastBS To LastDot - 1
  120.             s = s & name.Chars(i)
  121.         Next
  122.         GetName = s
  123.     End Function
  124. #End Region
  125. #Region "Eventi della windows form"
  126.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  127.         AddHandler TCP.DownloadProgressChanged, AddressOf PrgChngd
  128.         AddHandler TCP.DownloadFileCompleted, AddressOf PrgComplete
  129.     End Sub
  130.     Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
  131.         If lstDown.Items.Count > 0 Then
  132.             TCP.DownloadFileAsync(New Uri(DList(0).URL), DList(0).path)
  133.             NewDownload(DList(0).URL)
  134.         Else
  135.             MsgBox("Nessun elemento da scaricare è presente nella lista!", MsgBoxStyle.Exclamation, "Errore")
  136.         End If
  137.     End Sub
  138.     Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
  139.         If txtURL.Text = "" Or txtFile.Text = "" Then
  140.             MsgBox("Inserire prima l'indirizzo da cui scaricare il file e la locazione in cui copiarlo!", MsgBoxStyle.Exclamation, "Errore")
  141.             Exit Sub
  142.         End If
  143.  
  144.         lstDown.Items.Add(txtURL.Text & " da scaricare in " & txtFile.Text)
  145.         DList(lstDown.Items.Count - 1).path = txtFile.Text
  146.         DList(lstDown.Items.Count - 1).URL = txtURL.Text
  147.         txtURL.Text = ""
  148.         txtFile.Text = ""
  149.     End Sub
  150.     Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click
  151.         If lstDown.SelectedIndex > 0 Then
  152.             lstDown.Items.RemoveAt(lstDown.SelectedIndex)
  153.         End If
  154.         If lstDown.SelectedIndex = 0 Then
  155.             If MsgBox("Il file è attualemente in download. Bloccarlo?", MsgBoxStyle.Question + vbYesNo, "Download") = MsgBoxResult.Yes Then
  156.                 TCP.CancelAsync()
  157.                 lstDown.Items.RemoveAt(0)
  158.                 EndOfDownload()
  159.             End If
  160.         End If
  161.     End Sub
  162.     Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
  163.         TCP.CancelAsync()
  164.         EndOfDownload()
  165.     End Sub
  166.     Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowse.Click
  167.         Dim DefaultExt As String = GetExtension(txtURL.Text)
  168.         FOpen.Filter = "File " & DefaultExt & "|*." & DefaultExt & "|Tutti i file|*.*"
  169.  
  170.         If FOpen.ShowDialog = Windows.Forms.DialogResult.OK Then
  171.             txtFile.Text = FOpen.FileName
  172.         End If
  173.     End Sub
  174.     Private Sub cmdStopAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStopAll.Click
  175.         lstDown.Items.Clear()
  176.         TCP.CancelAsync()
  177.         EndOfDownload()
  178.     End Sub
  179.     Private Sub cmdInternet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInternet.Click
  180.         Me.Close()
  181.     End Sub
  182. #End Region
  183. End Class