Bonsoir,

j’arrive pas à télécharger 2 fichier à la fois avec listview. le premier fonctionne mais pas le deuxième. avez-vous une solution pour télécharger 2 fichier à la fois après a la suite.

voici en image :
Nom : Capture d’écran 2024-07-27 203929.png
Affichages : 175
Taille : 7,8 Ko

mon code from1 :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
....etc
For Each Item As ListViewItem In ListView1.CheckedItems
                        Download.InfoDownloader(Item.Text, Item.SubItems(1).Text, Folder.SelectedPath)
                    Next
                    Download.Show()
...etc
mon code avec download :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Private WithEvents _Downloader As New WebFileDownloader
    Private counter As Integer = 0
 
    Public Sub InfoDownloader(Names As String, URL As String, destination As String)
        Dim lvi As New ListViewItem(Names)
        lvi.UseItemStyleForSubItems = False
        lvi.SubItems.Add("Vérifications en cours....").ForeColor = Color.DarkBlue
        lvi.ImageIndex = 0
        LvDownloads.Items.Add(lvi)
        Task.Factory.StartNew(Sub()
                                  AddHandler _Downloader.DownloadProgressChanged, New WebFileDownloader.ProgressChangedEventHandler(AddressOf download_DownloadProgress)
                                  AddHandler _Downloader.DownloadCompleted, New WebFileDownloader.DownloadCompletedEventHandler(AddressOf download_DownloadComplete)
                                  _Downloader.DownloadFile(URL, Path.Combine(destination, Names & Path.GetExtension(URL)))
                              End Sub)
    End Sub
 
    Private Sub download_DownloadComplete(sender As Object, e As DownloadCompletedArgs)
        If e.ErrorMessage Then
            LvDownloads.Invoke(Sub()
                                   LvDownloads.Items(counter).SubItems(1).Text = "Avec succès..."
                                   LvDownloads.Items(counter).SubItems(1).ForeColor = Color.Blue
                               End Sub)
            If counter = LvDownloads.Items.Count Then
                counter += 1
            Else
                Exit Sub
            End If
        Else
            LvDownloads.Invoke(Sub()
                                   LvDownloads.Items(counter).SubItems(1).Text = "Erreur de téléchargement de la video"
                                   LvDownloads.Items(counter).SubItems(1).ForeColor = Color.Red
                               End Sub)
        End If
    End Sub
 
    Private Sub download_DownloadProgress(sender As Object, e As DownloadProgressArgs)
        On Error Resume Next
        LvDownloads.Invoke(Sub()
                               LvDownloads.Items(counter).SubItems(1).Text = "Téléchargement(" & FormatFileSize(e.BytesReceived) & " / " & FormatFileSize(e.TotalBytesReceived) & ") - " & e.ProgressPercentage & " %"
                               LvDownloads.Items(counter).SubItems(1).ForeColor = Color.Green
                           End Sub)
    End Sub
    Private Function FormatFileSize(byteCount As Long) As String
        Dim suf As String() = {"Bytes", "KB", "MB", "GB"}
        If byteCount = 0 Then Return "0" & suf(0)
        Dim bytes As Long = Math.Abs(byteCount)
        Dim place As Integer = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)))
        Dim num As Double = Math.Round(bytes / Math.Pow(1024, place), 1)
        Return (Math.Sign(byteCount) * num).ToString() & " " & suf(place)
    End Function
 
    Private Sub Download_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        System.Net.ServicePointManager.DefaultConnectionLimit = 2
    End Sub
End Class
ma classe WebFileDownloader :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
 
Imports System.Net
Imports System.IO
 
Class WebFileDownloader
 
    Public Delegate Sub ProgressChangedEventHandler(ByVal sender As Object, ByVal e As DownloadProgressArgs)
    Public Event DownloadProgressChanged As ProgressChangedEventHandler
    Public Delegate Sub DownloadCompletedEventHandler(ByVal sender As Object, ByVal e As DownloadCompletedArgs)
    Public Event DownloadCompleted As DownloadCompletedEventHandler
 
    Protected Overridable Sub OnDownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressArgs)
        RaiseEvent DownloadProgressChanged(Me, e)
    End Sub
 
    Protected Overridable Sub OnDownloadCompleted(ByVal sender As Object, ByVal e As DownloadCompletedArgs)
        RaiseEvent DownloadCompleted(Me, e)
    End Sub
 
    Sub DownloadFile(ByVal URL As String, ByVal Location As String)
        Try
            Dim uri As New Uri(URL)
            If ((uri.Scheme <> uri.UriSchemeHttp) And (uri.Scheme <> uri.UriSchemeHttps)) Then
                Throw New Exception("URL invalide. L'URL doit commencer par ""http://"" ou ""https://"".")
                Exit Sub
            End If
            Dim request As HttpWebRequest = CType(WebRequest.Create(New Uri(URL)), HttpWebRequest)
            With request
                .Method = "GET"
                .Timeout = 20000
                .KeepAlive = True
                .UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0"
            End With
            Using response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
                If response.StatusCode = HttpStatusCode.OK Then
                    Dim _ContentLenght As Integer = response.ContentLength
                    Using stream As Stream = response.GetResponseStream()
                        Using fs As New FileStream(Location, FileMode.Create, FileAccess.Write, FileShare.Write)
                            Dim buffer As Byte() = New Byte(4096 - 1) {}
                            Dim _TotalBytesReceived As Integer = 0
                            Dim isRead As Boolean = True
                            Do
                                Dim readsize As Integer = stream.Read(buffer, 0, buffer.Length)
                                If readsize = 0 Then
                                    isRead = False
                                    Exit Do
                                End If
                                _TotalBytesReceived += readsize
                                fs.Write(buffer, 0, readsize)
                                OnDownloadProgressChanged(Me, New DownloadProgressArgs(_TotalBytesReceived, _ContentLenght))
                            Loop While isRead
                        End Using
                    End Using
                End If
            End Using
            OnDownloadCompleted(Me, New DownloadCompletedArgs(True))
        Catch ex As WebException
            OnDownloadCompleted(Me, New DownloadCompletedArgs(False))
        End Try
    End Sub
 
End Class
 
Class DownloadProgressArgs
 
    Private _BytesReceived As Integer
    Private _TotalBytesReceived As Integer
 
    Public Sub New(ByVal BytesReceived As Integer, ByVal TotalBytesReceived As Integer)
        _BytesReceived = BytesReceived
        _TotalBytesReceived = TotalBytesReceived
    End Sub
 
    Public ReadOnly Property BytesReceived As Integer
        Get
            Return _BytesReceived
        End Get
    End Property
 
    Public ReadOnly Property TotalBytesReceived As Integer
        Get
            Return _TotalBytesReceived
        End Get
    End Property
 
    Public ReadOnly Property ProgressPercentage As Integer
        Get
            If _TotalBytesReceived > 0 Then
                Return Math.Ceiling((_BytesReceived / _TotalBytesReceived) * 100)
            Else
                Return 0
            End If
        End Get
    End Property
End Class
 
Class DownloadCompletedArgs
 
    Private _ErrorMessage As Boolean
 
    Sub New(ErrorMessage As Boolean)
        _ErrorMessage = ErrorMessage
    End Sub
 
    Public ReadOnly Property ErrorMessage As Boolean
        Get
            Return _ErrorMessage
        End Get
    End Property
End Class
pouvez-vous m'aider à télécharger 2 fichier a la fois. Merci d'avance