وللفائدة هذه هي الاكواد التي استخدمها بعيدا عن WebClient
قم بإستدعاء المكتبات التالية
في قسم التصاريح قم بتعريف المتغيرات التالية
كود زر التحمييل
إجراءات التحديث والتحميل
زر الإيقاف
زر التوقف المؤقت والاستمرار


قم بإستدعاء المكتبات التالية
PHP كود :
Imports System.Collections.Generic
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Net
Imports System.IO
Imports System.Threading
PHP كود :
Private thrDownload As Thread
Private strResponse As Stream
Private strLocal As Stream
Private webRequest As HttpWebRequest
Private webResponse As HttpWebResponse
Private Shared PercentProgress As Integer
Private Delegate Sub UpdateProgessCallback(BytesRead As Int64, TotalBytes As Int64)
Private goPause As Boolean = False
PHP كود :
Private Sub btnDownload_Click(sender As Object, e As EventArgs)
If thrDownload IsNot Nothing AndAlso thrDownload.ThreadState = ThreadState.Running Then
MessageBox.Show _
("A download is already running", _
"Download in progress", MessageBoxButtons.OK, MessageBoxIcon.[Error])
Else
lblProgress.Text = "Download Starting"
thrDownload = New Thread(New ParameterizedThreadStart(AddressOf Download))
thrDownload.Start(0)
btnPauseResume.Enabled = True
btnPauseResume.Text = "Pause"
End If
End Sub
PHP كود :
Private Sub UpdateProgress(BytesRead As Int64, TotalBytes As Int64)
PercentProgress = Convert.ToInt32((BytesRead * 100) / TotalBytes)
prgDownload.Value = PercentProgress
lblProgress.Text = "Downloaded " + BytesRead + " out of " + TotalBytes + " (" + PercentProgress + "%)"
End Sub
Private Sub Download(startPoint As Object)
Try
Dim startPointInt As Integer = Convert.ToInt32(startPoint)
webRequest = DirectCast(WebRequest.Create(txtUrl.Text), HttpWebRequest)
webRequest.AddRange(startPointInt)
webRequest.Credentials = CredentialCache.DefaultCredentials
webResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
Dim fileSize As Int64 = webResponse.ContentLength
strResponse = webResponse.GetResponseStream()
If startPointInt = 0 Then
strLocal = New FileStream(txtPath.Text, FileMode.Create, FileAccess.Write, FileShare.None)
Else
strLocal = New FileStream(txtPath.Text, FileMode.Append, FileAccess.Write, FileShare.None)
End If
Dim bytesSize As Integer = 0
Dim downBuffer As Byte() = New Byte(2047) {}
While (InlineAssignHelper(bytesSize, strResponse.Read(downBuffer, 0, downBuffer.Length))) > 0
strLocal.Write(downBuffer, 0, bytesSize)
Me.Invoke(New UpdateProgessCallback(AddressOf Me.UpdateProgress), _
New Object() {strLocal.Length, fileSize + startPointInt})
If goPause = True Then
Exit While
End If
End While
Finally
strResponse.Close()
strLocal.Close()
End Try
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
target = value
Return value
End Function
Public Sub New()
InitializeComponent()
End Sub
PHP كود :
Private Sub btnStop_Click(sender As Object, e As EventArgs)
thrDownload.Abort()
webResponse.Close()
strResponse.Close()
strLocal.Close()
prgDownload.Value = 0
lblProgress.Text = "Download Stopped"
btnPauseResume.Enabled = False
End Sub
PHP كود :
Private Sub btnPauseResume_Click(sender As Object, e As EventArgs)
If thrDownload IsNot Nothing Then
If btnPauseResume.Text = "Pause" Then
goPause = True
btnPauseResume.Text = "Resume"
webResponse.Close()
strResponse.Close()
strLocal.Close()
thrDownload.Abort()
Else
goPause = False
btnPauseResume.Text = "Pause"
Dim startPoint As Long = 0
If File.Exists(txtPath.Text) Then
startPoint = New FileInfo(txtPath.Text).Length
Else
MessageBox.Show("The file you choosed to resume doesn't exist.", _
"Could not resume", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End If
lblProgress.Text = "Download Resuming"
thrDownload = New Thread(New ParameterizedThreadStart(AddressOf Download))
thrDownload.Start(startPoint)
btnPauseResume.Enabled = True
End If
Else
MessageBox.Show("A download does not appear to be in progress.", _
"Could not pause", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End If
End Sub


{ يَرْفَعِ اللَّهُ الَّذِينَ آمَنُوا مِنكُمْ وَالَّذِينَ أُوتُوا الْعِلْمَ دَرَجَاتٍ }
" Mohamed M. Bedair - " Abu Anas
Genius Live , Egypt

