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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
|
Imports System.Net
Imports SimpleDownloadFile
Public Class Form1
Dim Repertoire, Identifiant, Password, FichierIni As String
Dim Url1, Url2, Url3, Url4, Url5 As String
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Integer
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As System.Text.StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Delegate Sub ChangeTextsSafe(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer)
Delegate Sub DownloadCompleteSafe(ByVal cancelled As Boolean)
Function EcrireINI(ByVal entete As String, ByVal variable As String, ByVal valeur As String, ByVal fichier As String)
On Error Resume Next
valeur = Trim(valeur)
WritePrivateProfileString(entete, variable, valeur, fichier)
End Function
Function LireINI(ByVal Entete As String, ByVal Variable As String, ByVal Ini As String) As String
Dim Deff As String = ""
Try
Dim StrBuild As New System.Text.StringBuilder(90)
Dim Ret As Integer = GetPrivateProfileString(Entete, Variable, Deff, StrBuild, 90, Ini)
Return Trim(StrBuild.ToString)
Catch
Return Trim(Deff)
End Try
End Function
Sub AfficherMasquerDownload(ByVal Download As Integer, ByVal Afficher As Integer)
Select Case Download
Case 1
If Afficher = 0 Then : TexteUrl1.Visible = False : Else : TexteUrl1.Visible = True : End If
If Afficher = 0 Then : TexteSize1.Visible = False : Else : TexteSize1.Visible = True : End If
If Afficher = 0 Then : Size1.Visible = False : Else : Size1.Visible = True : End If
If Afficher = 0 Then : Index1.Visible = False : Else : Index1.Visible = True : End If
If Afficher = 0 Then : TexteDownload1.Visible = False : Else : TexteDownload1.Visible = True : End If
If Afficher = 0 Then : Down1.Visible = False : Else : Down1.Visible = True : End If
If Afficher = 0 Then : Pourcentage1.Visible = False : Else : Pourcentage1.Visible = True : End If
If Afficher = 0 Then : ProgressBar1.Visible = False : Else : ProgressBar1.Visible = True : End If
Case 2
If Afficher = 0 Then : TexteUrl2.Visible = False : Else : TexteUrl2.Visible = True : End If
If Afficher = 0 Then : TexteSize2.Visible = False : Else : TexteSize2.Visible = True : End If
If Afficher = 0 Then : Size2.Visible = False : Else : Size2.Visible = True : End If
If Afficher = 0 Then : Index2.Visible = False : Else : Index2.Visible = True : End If
If Afficher = 0 Then : TexteDownload2.Visible = False : Else : TexteDownload2.Visible = True : End If
If Afficher = 0 Then : Down2.Visible = False : Else : Down2.Visible = True : End If
If Afficher = 0 Then : Pourcentage2.Visible = False : Else : Pourcentage2.Visible = True : End If
If Afficher = 0 Then : ProgressBar2.Visible = False : Else : ProgressBar2.Visible = True : End If
Case 3
If Afficher = 0 Then : TexteUrl3.Visible = False : Else : TexteUrl3.Visible = True : End If
If Afficher = 0 Then : TexteSize3.Visible = False : Else : TexteSize3.Visible = True : End If
If Afficher = 0 Then : Size3.Visible = False : Else : Size3.Visible = True : End If
If Afficher = 0 Then : Index3.Visible = False : Else : Index3.Visible = True : End If
If Afficher = 0 Then : TexteDownload3.Visible = False : Else : TexteDownload3.Visible = True : End If
If Afficher = 0 Then : Down3.Visible = False : Else : Down3.Visible = True : End If
If Afficher = 0 Then : Pourcentage3.Visible = False : Else : Pourcentage3.Visible = True : End If
If Afficher = 0 Then : ProgressBar3.Visible = False : Else : ProgressBar3.Visible = True : End If
Case 4
If Afficher = 0 Then : TexteUrl4.Visible = False : Else : TexteUrl4.Visible = True : End If
If Afficher = 0 Then : TexteSize4.Visible = False : Else : TexteSize4.Visible = True : End If
If Afficher = 0 Then : Size4.Visible = False : Else : Size4.Visible = True : End If
If Afficher = 0 Then : Index4.Visible = False : Else : Index4.Visible = True : End If
If Afficher = 0 Then : TexteDownload4.Visible = False : Else : TexteDownload4.Visible = True : End If
If Afficher = 0 Then : Down4.Visible = False : Else : Down4.Visible = True : End If
If Afficher = 0 Then : Pourcentage4.Visible = False : Else : Pourcentage4.Visible = True : End If
If Afficher = 0 Then : ProgressBar4.Visible = False : Else : ProgressBar4.Visible = True : End If
Case 5
If Afficher = 0 Then : TexteUrl5.Visible = False : Else : TexteUrl5.Visible = True : End If
If Afficher = 0 Then : TexteSize5.Visible = False : Else : TexteSize5.Visible = True : End If
If Afficher = 0 Then : Size5.Visible = False : Else : Size5.Visible = True : End If
If Afficher = 0 Then : Index5.Visible = False : Else : Index5.Visible = True : End If
If Afficher = 0 Then : TexteDownload5.Visible = False : Else : TexteDownload5.Visible = True : End If
If Afficher = 0 Then : Down5.Visible = False : Else : Down5.Visible = True : End If
If Afficher = 0 Then : Pourcentage5.Visible = False : Else : Pourcentage5.Visible = True : End If
If Afficher = 0 Then : ProgressBar5.Visible = False : Else : ProgressBar5.Visible = True : End If
End Select
End Sub
Private Sub Download1()
Dim Absent As Boolean
Dim Fichier As String = Nothing
Dim Rep As String = Nothing
Timer1.Stop()
For Compte = 0 To ListView1.Items.Count - 1
Url1 = ListView1.Items(Compte).Text
If Url1 <> Url2 And Url1 <> Url3 And Url1 <> Url4 And Url1 <> Url5 Then
Dim pos As Integer = Url1.LastIndexOf("/")
Dim T = LireINI("Menu", Str(Compte + 1), FichierIni)
Dim W As String = T.Substring(Len(T) - 2)
If W = "*1" Then
Absent = False
Else
Absent = True
AfficherMasquerDownload(1, 1) '<=Modifier
ListView1.Items(Compte).ForeColor = Color.Red '<=Modifier
ListView1.Items(Compte).BackColor = Color.Yellow '<=Modifier
Index1.Text = Compte '<=Modifier(Label Index)
Exit For
End If
End If
Next
If Absent = True Then
Timer1.Start()
TexteUrl1.Text = Url1
Rep = TexteUrl1.Text.Substring(7)
Rep = Repertoire & Microsoft.VisualBasic.Left(Rep, Rep.LastIndexOf("/")) & "\"
System.IO.Directory.CreateDirectory(Rep)
Fichier = TexteUrl1.Text.Substring(TexteUrl1.Text.LastIndexOf("/") + 1)
BackgroundWorker1.RunWorkerAsync(New Object() {Rep & Fichier})
End If
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim Fichier As String = e.Argument(0)
Dim theResponse As HttpWebResponse
Dim theRequest As HttpWebRequest
Dim Credit As NetworkCredential = New NetworkCredential(Identifiant, Password)
Try 'Checks if the file exist
' theRequest.Credentials = Credit
theRequest = WebRequest.Create(TexteUrl1.Text)
theResponse = theRequest.GetResponse
Catch ex As Exception
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete1)
Me.Invoke(cancelDelegate, True)
Exit Sub
End Try
Dim length As Integer = theResponse.ContentLength 'Size of the response (in bytes)
Dim safedelegate As New ChangeTextsSafe(AddressOf ProgressDownload1)
Me.Invoke(safedelegate, length, 0, 0) 'Invoke the TreadsafeDelegate
Dim writeStream As New IO.FileStream(Fichier, IO.FileMode.Create)
Dim nRead As Integer
Dim readings As Integer = 0
Do
If BackgroundWorker1.CancellationPending Then 'If user abort download
Exit Do
End If
Dim readBytes(4095) As Byte
Dim bytesread As Short = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
nRead += bytesread
Dim percent As Integer = CInt(((nRead / 1024) * 100) / (length / 1024))
Me.Invoke(safedelegate, length, nRead, percent)
If bytesread = 0 Then Exit Do
writeStream.Write(readBytes, 0, bytesread)
readings += 1
Loop
theResponse.GetResponseStream.Close()
writeStream.Close()
If Me.BackgroundWorker1.CancellationPending Then
IO.File.Delete(Fichier)
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete1)
Me.Invoke(cancelDelegate, True)
Exit Sub
End If
Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete1)
Me.Invoke(completeDelegate, False)
End Sub
Public Sub ProgressDownload1(ByVal length As Integer, ByVal position As Integer, ByVal percent As Integer)
Size1.Text = Math.Round((length / 1024 / 1024), 2) & " MO"
Down1.Text = Math.Round((position / 1024 / 1024), 2) & " MO"
Me.ProgressBar1.Value = percent
Pourcentage1.Text = percent & " %"
End Sub
Public Sub DownloadComplete1(ByVal cancelled As Boolean)
TexteUrl1.Text = ""
Down1.Text = ""
Pourcentage1.Text = ""
ProgressBar1.Value = 0
Dim Index As Integer = Val(Index1.Text) + 1
If cancelled = False Then : EcrireINI("Menu", Str(Index), LireINI("Menu", Str(Index), FichierIni) & "*1", FichierIni) : End If
ListView1.Items(Index - 1).ForeColor = Color.Lime '<=Modifier
ListView1.Items(Index - 1).BackColor = Color.Teal '<=Modifier
Dim a As Integer = Val(Label2.Text) + 1
Label2.Text = a
AfficherMasquerDownload(1, 0)
End Sub
Private Sub Download2()
Dim Absent As Boolean
Dim Fichier As String = Nothing
Dim Rep As String = Nothing
Timer1.Stop()
For Compte = 0 To ListView1.Items.Count - 1
Url2 = ListView1.Items(Compte).Text
If Url2 <> Url1 And Url2 <> Url3 And Url2 <> Url4 And Url2 <> Url5 Then
Dim pos As Integer = Url1.LastIndexOf("/")
Dim T = LireINI("Menu", Str(Compte + 1), FichierIni)
Dim W As String = T.Substring(Len(T) - 2)
If W = "*1" Then
Absent = False
Else
Absent = True
AfficherMasquerDownload(2, 1) '<=Modifier
ListView1.Items(Compte).ForeColor = Color.Red '<=Modifier
ListView1.Items(Compte).BackColor = Color.Yellow '<=Modifier
Index2.Text = Compte '<=Modifier(Label Index)
Exit For
End If
End If
Next
If Absent = True Then
Timer1.Start()
TexteUrl2.Text = Url2
Rep = TexteUrl2.Text.Substring(7)
Rep = Repertoire & Microsoft.VisualBasic.Left(Rep, Rep.LastIndexOf("/")) & "\"
System.IO.Directory.CreateDirectory(Rep)
Fichier = TexteUrl2.Text.Substring(TexteUrl2.Text.LastIndexOf("/") + 1)
BackgroundWorker2.RunWorkerAsync(New Object() {Rep & Fichier})
End If
End Sub
Private Sub BackgroundWorker2_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
Dim Fichier As String = e.Argument(0)
Dim theResponse As HttpWebResponse
Dim theRequest As HttpWebRequest
Dim Credit As NetworkCredential = New NetworkCredential(Identifiant, Password)
Try
' theRequest.Credentials = Credit
theRequest = WebRequest.Create(TexteUrl1.Text)
theResponse = theRequest.GetResponse
Catch ex As Exception
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete2)
Me.Invoke(cancelDelegate, True)
Exit Sub
End Try
Dim length As Integer = theResponse.ContentLength
Dim safedelegate As New ChangeTextsSafe(AddressOf ProgressDownload2)
Me.Invoke(safedelegate, length, 0, 0)
Dim writeStream As New IO.FileStream(Fichier, IO.FileMode.Create)
Dim nRead As Integer
Dim readings As Integer = 0
Do
If BackgroundWorker2.CancellationPending Then
Exit Do
End If
Dim readBytes(4095) As Byte
Dim bytesread As Short = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
nRead += bytesread
Dim percent As Integer = CInt(((nRead / 1024) * 100) / (length / 1024))
Me.Invoke(safedelegate, length, nRead, percent)
If bytesread = 0 Then Exit Do
writeStream.Write(readBytes, 0, bytesread)
readings += 1
Loop
theResponse.GetResponseStream.Close()
writeStream.Close()
If Me.BackgroundWorker2.CancellationPending Then
IO.File.Delete(Fichier)
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete2)
Me.Invoke(cancelDelegate, True)
Exit Sub
End If
Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete2)
Me.Invoke(completeDelegate, False)
End Sub
Public Sub ProgressDownload2(ByVal length As Integer, ByVal position As Integer, ByVal percent As Integer)
Size2.Text = Math.Round((length / 1024 / 1024), 2) & " MO"
Down2.Text = Math.Round((position / 1024 / 1024), 2) & " MO"
ProgressBar2.Value = percent
Pourcentage2.Text = percent & " %"
End Sub
Public Sub DownloadComplete2(ByVal cancelled As Boolean)
TexteUrl2.Text = ""
Down2.Text = ""
Pourcentage2.Text = ""
ProgressBar2.Value = 0
Dim Index As Integer = Val(Index2.Text) + 1
If cancelled = False Then : EcrireINI("Menu", Str(Index), LireINI("Menu", Str(Index), FichierIni) & "*1", FichierIni) : End If
ListView1.Items(Index - 1).ForeColor = Color.Lime '<=Modifier
ListView1.Items(Index - 1).BackColor = Color.Teal '<=Modifier
Dim a As Integer = Val(Label2.Text) + 1
Label2.Text = a
AfficherMasquerDownload(2, 0)
End Sub
Private Sub Download3()
Dim Absent As Boolean
Dim Fichier As String = Nothing
Dim Rep As String = Nothing
Timer1.Stop()
For Compte = 0 To ListView1.Items.Count - 1
Url3 = ListView1.Items(Compte).Text
If Url3 <> Url1 And Url3 <> Url2 And Url3 <> Url4 And Url3 <> Url5 Then
Dim pos As Integer = Url3.LastIndexOf("/")
Dim T = LireINI("Menu", Str(Compte + 1), FichierIni)
Dim W As String = T.Substring(Len(T) - 2)
If W = "*1" Then
Absent = False
Else
Absent = True
AfficherMasquerDownload(3, 1) '<=Modifier
ListView1.Items(Compte).ForeColor = Color.Red '<=Modifier
ListView1.Items(Compte).BackColor = Color.Yellow '<=Modifier
Index3.Text = Compte '<=Modifier(Label Index)
Exit For
End If
End If
Next
If Absent = True Then
Timer1.Start()
TexteUrl3.Text = Url3
Rep = TexteUrl3.Text.Substring(7)
Rep = Repertoire & Microsoft.VisualBasic.Left(Rep, Rep.LastIndexOf("/")) & "\"
System.IO.Directory.CreateDirectory(Rep)
Fichier = TexteUrl3.Text.Substring(TexteUrl3.Text.LastIndexOf("/") + 1)
BackgroundWorker3.RunWorkerAsync(New Object() {Rep & Fichier})
End If
End Sub
Private Sub BackgroundWorker3_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker3.DoWork
Dim Fichier As String = e.Argument(0)
Dim theResponse As HttpWebResponse
Dim theRequest As HttpWebRequest
Dim Credit As NetworkCredential = New NetworkCredential(Identifiant, Password)
Try
' theRequest.Credentials = Credit
theRequest = WebRequest.Create(TexteUrl3.Text)
theResponse = theRequest.GetResponse
Catch ex As Exception
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete3)
Me.Invoke(cancelDelegate, True)
Exit Sub
End Try
Dim length As Integer = theResponse.ContentLength
Dim safedelegate As New ChangeTextsSafe(AddressOf ProgressDownload3)
Me.Invoke(safedelegate, length, 0, 0)
Dim writeStream As New IO.FileStream(Fichier, IO.FileMode.Create)
Dim nRead As Integer
Dim readings As Integer = 0
Do
If BackgroundWorker3.CancellationPending Then
Exit Do
End If
Dim readBytes(4095) As Byte
Dim bytesread As Short = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
nRead += bytesread
Dim percent As Integer = CInt(((nRead / 1024) * 100) / (length / 1024))
Me.Invoke(safedelegate, length, nRead, percent)
If bytesread = 0 Then Exit Do
writeStream.Write(readBytes, 0, bytesread)
readings += 1
Loop
theResponse.GetResponseStream.Close()
writeStream.Close()
If Me.BackgroundWorker3.CancellationPending Then
IO.File.Delete(Fichier)
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete3)
Me.Invoke(cancelDelegate, True)
Exit Sub
End If
Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete3)
Me.Invoke(completeDelegate, False)
End Sub
Public Sub ProgressDownload3(ByVal length As Integer, ByVal position As Integer, ByVal percent As Integer)
Size3.Text = Math.Round((length / 1024 / 1024), 2) & " MO"
Down3.Text = Math.Round((position / 1024 / 1024), 2) & " MO"
ProgressBar3.Value = percent
Pourcentage3.Text = percent & " %"
End Sub
Public Sub DownloadComplete3(ByVal cancelled As Boolean)
TexteUrl3.Text = ""
Down3.Text = ""
Pourcentage3.Text = ""
ProgressBar3.Value = 0
Dim Index As Integer = Val(Index3.Text) + 1
If cancelled = False Then : EcrireINI("Menu", Str(Index), LireINI("Menu", Str(Index), FichierIni) & "*1", FichierIni) : End If
ListView1.Items(Index - 1).ForeColor = Color.Lime '<=Modifier
ListView1.Items(Index - 1).BackColor = Color.Teal '<=Modifier
Dim a As Integer = Val(Label2.Text) + 1
Label2.Text = a
AfficherMasquerDownload(3, 0)
End Sub
Private Sub Download4()
Dim Absent As Boolean
Dim Fichier As String = Nothing
Dim Rep As String = Nothing
Timer1.Stop()
For Compte = 0 To ListView1.Items.Count - 1
Url4 = ListView1.Items(Compte).Text
If Url4 <> Url1 And Url4 <> Url2 And Url4 <> Url3 And Url4 <> Url5 Then
Dim pos As Integer = Url4.LastIndexOf("/")
Dim T = LireINI("Menu", Str(Compte + 1), FichierIni)
Dim W As String = T.Substring(Len(T) - 2)
If W = "*1" Then
Absent = False
Else
Absent = True
AfficherMasquerDownload(4, 1)
ListView1.Items(Compte).ForeColor = Color.Red
ListView1.Items(Compte).BackColor = Color.Yellow
Index4.Text = Compte '<=Modifier(Label Index)
Exit For
End If
End If
Next
If Absent = True Then
Timer1.Start()
TexteUrl4.Text = Url4
Rep = TexteUrl4.Text.Substring(7)
Rep = Repertoire & Microsoft.VisualBasic.Left(Rep, Rep.LastIndexOf("/")) & "\"
System.IO.Directory.CreateDirectory(Rep)
Fichier = TexteUrl4.Text.Substring(TexteUrl4.Text.LastIndexOf("/") + 1)
BackgroundWorker4.RunWorkerAsync(New Object() {Rep & Fichier})
End If
End Sub
Private Sub BackgroundWorker4_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker4.DoWork
Dim Fichier As String = e.Argument(0)
Dim theResponse As HttpWebResponse
Dim theRequest As HttpWebRequest
Dim Credit As NetworkCredential = New NetworkCredential(Identifiant, Password)
Try
' theRequest.Credentials = Credit
theRequest = WebRequest.Create(TexteUrl4.Text)
theResponse = theRequest.GetResponse
Catch ex As Exception
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete4)
Me.Invoke(cancelDelegate, True)
Exit Sub
End Try
Dim length As Integer = theResponse.ContentLength
Dim safedelegate As New ChangeTextsSafe(AddressOf ProgressDownload4)
Me.Invoke(safedelegate, length, 0, 0)
Dim writeStream As New IO.FileStream(Fichier, IO.FileMode.Create)
Dim nRead As Integer
Dim readings As Integer = 0
Do
If BackgroundWorker4.CancellationPending Then
Exit Do
End If
Dim readBytes(4095) As Byte
Dim bytesread As Short = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
nRead += bytesread
Dim percent As Integer = CInt(((nRead / 1024) * 100) / (length / 1024))
Me.Invoke(safedelegate, length, nRead, percent)
If bytesread = 0 Then Exit Do
writeStream.Write(readBytes, 0, bytesread)
readings += 1
Loop
theResponse.GetResponseStream.Close()
writeStream.Close()
If Me.BackgroundWorker4.CancellationPending Then
IO.File.Delete(Fichier)
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete4)
Me.Invoke(cancelDelegate, True)
Exit Sub
End If
Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete4)
Me.Invoke(completeDelegate, False)
End Sub
Public Sub ProgressDownload4(ByVal length As Integer, ByVal position As Integer, ByVal percent As Integer)
Size4.Text = Math.Round((length / 1024 / 1024), 2) & " MO"
Down4.Text = Math.Round((position / 1024 / 1024), 2) & " MO"
ProgressBar4.Value = percent
Pourcentage4.Text = percent & " %"
End Sub
Public Sub DownloadComplete4(ByVal cancelled As Boolean)
TexteUrl4.Text = ""
Down4.Text = ""
Pourcentage4.Text = ""
ProgressBar4.Value = 0
Dim Index As Integer = Val(Index4.Text) + 1
If cancelled = False Then : EcrireINI("Menu", Str(Index), LireINI("Menu", Str(Index), FichierIni) & "*1", FichierIni) : End If
ListView1.Items(Index - 1).ForeColor = Color.Lime '<=Modifier
ListView1.Items(Index - 1).BackColor = Color.Teal '<=Modifier
Dim a As Integer = Val(Label2.Text) + 1
Label2.Text = a
AfficherMasquerDownload(4, 0)
End Sub
Private Sub Download5()
Dim Absent As Boolean
Dim Fichier As String = Nothing
Dim Rep As String = Nothing
Timer1.Stop()
For Compte = 0 To ListView1.Items.Count - 1
Url5 = ListView1.Items(Compte).Text
If Url5 <> Url1 And Url5 <> Url2 And Url5 <> Url3 And Url5 <> Url4 Then
Dim pos As Integer = Url5.LastIndexOf("/")
Dim T = LireINI("Menu", Str(Compte + 1), FichierIni)
Dim W As String = T.Substring(Len(T) - 2)
If W = "*1" Then
Absent = False
Else
Absent = True
AfficherMasquerDownload(5, 1)
ListView1.Items(Compte).ForeColor = Color.Red
ListView1.Items(Compte).BackColor = Color.Yellow
Index5.Text = Compte
Exit For
End If
End If
Next
If Absent = True Then
Timer1.Start()
TexteUrl5.Text = Url5
Rep = TexteUrl5.Text.Substring(7)
Rep = Repertoire & Microsoft.VisualBasic.Left(Rep, Rep.LastIndexOf("/")) & "\"
System.IO.Directory.CreateDirectory(Rep)
Fichier = TexteUrl5.Text.Substring(TexteUrl5.Text.LastIndexOf("/") + 1)
BackgroundWorker5.RunWorkerAsync(New Object() {Rep & Fichier})
End If
End Sub
Private Sub BackgroundWorker5_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker5.DoWork
Dim Fichier As String = e.Argument(0)
Dim theResponse As HttpWebResponse
Dim theRequest As HttpWebRequest
Dim Credit As NetworkCredential = New NetworkCredential(Identifiant, Password)
Try
' theRequest.Credentials = Credit
theRequest = WebRequest.Create(TexteUrl5.Text)
theResponse = theRequest.GetResponse
Catch ex As Exception
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete5)
Me.Invoke(cancelDelegate, True)
Exit Sub
End Try
Dim length As Integer = theResponse.ContentLength
Dim safedelegate As New ChangeTextsSafe(AddressOf ProgressDownload5)
Me.Invoke(safedelegate, length, 0, 0)
Dim writeStream As New IO.FileStream(Fichier, IO.FileMode.Create)
Dim nRead As Integer
Dim readings As Integer = 0
Do
If BackgroundWorker5.CancellationPending Then
Exit Do
End If
Dim readBytes(4095) As Byte
Dim bytesread As Short = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
nRead += bytesread
Dim percent As Integer = CInt(((nRead / 1024) * 100) / (length / 1024))
Me.Invoke(safedelegate, length, nRead, percent)
If bytesread = 0 Then Exit Do
writeStream.Write(readBytes, 0, bytesread)
readings += 1
Loop
theResponse.GetResponseStream.Close()
writeStream.Close()
If BackgroundWorker5.CancellationPending Then
IO.File.Delete(Fichier)
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete5)
Me.Invoke(cancelDelegate, True)
Exit Sub
End If
Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete5)
Me.Invoke(completeDelegate, False)
End Sub
Public Sub ProgressDownload5(ByVal length As Integer, ByVal position As Integer, ByVal percent As Integer)
Size5.Text = Math.Round((length / 1024 / 1024), 2) & " MO"
Down5.Text = Math.Round((position / 1024 / 1024), 2) & " MO"
ProgressBar5.Value = percent
Pourcentage5.Text = percent & " %"
End Sub
Public Sub DownloadComplete5(ByVal cancelled As Boolean)
TexteUrl5.Text = ""
Down5.Text = ""
Pourcentage5.Text = ""
ProgressBar5.Value = 0
Dim Index As Integer = Val(Index5.Text) + 1
If cancelled = False Then : EcrireINI("Menu", Str(Index), LireINI("Menu", Str(Index), FichierIni) & "*1", FichierIni) : End If
ListView1.Items(Index - 1).ForeColor = Color.Lime '<=Modifier
ListView1.Items(Index - 1).BackColor = Color.Teal '<=Modifier
Dim a As Integer = Val(Label2.Text) + 1
Label2.Text = a
AfficherMasquerDownload(5, 0)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AfficherMasquerDownload(1, 0)
AfficherMasquerDownload(2, 0)
AfficherMasquerDownload(3, 0)
AfficherMasquerDownload(4, 0)
AfficherMasquerDownload(5, 0)
Dim MyFolderBrowser As New System.Windows.Forms.FolderBrowserDialog
MyFolderBrowser.Description = "Selectionnez le répertoire de destination:"
MyFolderBrowser.SelectedPath = "E:\"
Dim dlgResult As DialogResult = MyFolderBrowser.ShowDialog()
If dlgResult = Windows.Forms.DialogResult.OK Then
Repertoire = MyFolderBrowser.SelectedPath + "\"
End If
FichierIni = Repertoire & "Fichiers.ini"
Dim Nombre As Integer = Val(LireINI("Menu", "Nombre", FichierIni))
Label3.Text = Nombre
ListView1.Focus()
Dim Url1 As String
For Compte = 0 To Nombre - 1
Url1 = LireINI("Menu", Str(Compte + 1), FichierIni)
Dim pos As Integer = Url1.LastIndexOf("*")
If Microsoft.VisualBasic.Right(Url1, 2) = "*1" Then
If pos > 0 Then : Url1 = Microsoft.VisualBasic.Left(Url1, pos) : End If
ListView1.Items.Add(Url1)
Dim a As Integer = Val(Label2.Text) + 1
Label2.Text = a
ListView1.Items(Compte).Selected = True
ListView1.Items(Compte).ForeColor = Color.Lime
Else
ListView1.Items.Add(Url1)
End If
Next
If ListView1.Items.Count > 0 Then
ListView1.MultiSelect = False
ListView1.Items(0).Focused = True
ListView1.Items(0).Selected = True
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Download1()
Download2()
Download3()
Download4()
Download5()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If BackgroundWorker1.IsBusy Then:Else : Download1():End If
If BackgroundWorker2.IsBusy Then:Else : Download2():End If
If BackgroundWorker3.IsBusy Then:Else : Download3():End If
If BackgroundWorker4.IsBusy Then : Else : Download4() : End If
If BackgroundWorker5.IsBusy Then : Else : Download5() : End If
End Sub
End Class |