الان الاشكالية الاولى واللتي لولا الله ثم الاخ Taha Okla لن اصل للحل بسرعة
الاشكالية اللي الخروج من for عند الانتهاء
الاجزاء لا اشكال فيها يتم دمجها وتشغيلها بشكل ممتاز لكن الفضول يحتم علي الوصول الى مابعد For لان فيها اجراءات مثل تفعيل الزر واسناد نص ما الى اداة تسمية
التالي كلاس الفورم لمن لا يريد تحميل المرفق:
PHP كود :
Imports System.IO
Imports System.Net
Imports System.Net.Mime
Public Class Form1
Dim url As String
Dim files As New List(Of String)
Dim folder As String
Dim th1 As Threading.Thread, th2 As Threading.Thread
Dim astop As Boolean
Dim imax As Long = 0
Dim mb As Long = (1024) ^ 2
Dim b() As Byte
Dim mo As Long = 0
Dim filex As Integer = 0
Private Function folderfilessize() As Long
Dim s As Long = 0
Dim d As New DirectoryInfo(folder)
For Each fl As FileInfo In d.GetFiles
s += fl.Length
Next
Return s
End Function
Private Function flen(path As String) As Long
Return New FileInfo(path).Length
End Function
Private Function folderfiles() As Integer
Dim d As New IO.DirectoryInfo(folder)
Return d.GetFiles.Count
End Function
Private Sub download(Optional ByVal txt As String = Nothing) ', b1 As Long, blast As Long)
Dim req As HttpWebRequest = HttpWebRequest.Create(New Uri(url))
Dim res As HttpWebResponse = req.GetResponse
Dim filesize As Long = res.ContentLength
mo = (filesize Mod imax)
Dim rs As Stream = res.GetResponseStream
Dim lastindex As Long = 0
'Dim filex As Integer = 0
Dim i As Long = 0
Dim pindex As Long = 0
Dim br As New BinaryReader(rs)
nxt:
Dim fl As New IO.FileInfo(files(filex))
dgv1.Invoke(Sub() dgv1.CurrentCell = dgv1.Rows(filex).Cells(0))
Dim fs As New FileStream(fl.FullName, FileMode.Append)
Dim bw As New BinaryWriter(fs)
For i = lastindex To filesize - 1
If astop = True Then Exit For
lastindex = i
bw.Write(br.ReadBytes(pindex))
If ((filesize - bw.BaseStream.Position) + imax) >= imax Then
pindex = (imax / 1024)
Else
pindex = mo ' (filesize Mod imax)
End If
If (flen(fl.FullName) = imax) And (filex < (files.Count - 1)) Then
filex += 1
GoTo nxt
ElseIf (flen(fl.FullName) >= mo) And (filex >= (files.Count - 1)) Then
Exit For
End If
Next
fl = Nothing
fs.Close()
bw.Close()
fs.Dispose()
bw.Dispose()
th1.Abort()
th2.Abort()
btnStart.Invoke(Sub() btnStart.Enabled = True)
If folderfiles() = files.Count Then
lblalert.Invoke(Sub() lblalert.ForeColor = Color.Green)
lblalert.Invoke(Sub() lblalert.Text = "تم التحميل بنجاح")
Else
lblalert.Invoke(Sub() lblalert.ForeColor = Color.Red)
lblalert.Invoke(Sub() lblalert.Text = "تم الايقاف ")
End If
End Sub
Private Sub makelist()
files.Clear()
Dim req As HttpWebRequest = HttpWebRequest.Create(New Uri(url))
Dim res As HttpWebResponse = req.GetResponse
ReDim Preserve b(res.ContentLength)
lblsize.Invoke(Sub() lblsize.Text = CInt(res.ContentLength) / mb)
Dim mi = (res.Headers("Content-Disposition"))
Dim refilename As String = (New ContentDisposition(mi).FileName)
Dim gro As Long = (res.ContentLength \ imax)
Dim mo As Long = (res.ContentLength Mod imax)
req.Abort()
res.Close()
Dim all As Long
If mo > 0 Then
all = (gro + 1)
ElseIf mo = 0 Then
all = gro
End If
Dim newfile As String = ""
For a As Integer = 1 To all
newfile = folder & refilename & ".Total" & all & "part" & (a).ToString(StrDup(Len(gro.ToString), "0"))
files.Add(newfile)
dgv1.Invoke(Sub() dgv1.Rows.Add())
Next
newfile = Nothing
all = Nothing
gro = Nothing
mi = Nothing
mo = Nothing
refilename = Nothing
th2 = New Threading.Thread(AddressOf download)
th2.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
For ii As Integer = 0 To files.Count - 1
Dim f As New IO.FileInfo(files(ii))
If IO.File.Exists(f.FullName) = True Then
If (mo = 0) And filex < (files.Count - 1) Then
dgv1.Rows(ii).Cells(0).Value = (imax / 1024).ToString & " kb"
ElseIf (mo > 0) And filex < (files.Count - 1) Then
dgv1.Rows(ii).Cells(0).Value = (imax / 1024).ToString & " kb"
ElseIf (mo > 0) And filex = (files.Count - 1) Then
dgv1.Rows(ii).Cells(0).Value = (mo / 1024).ToString & " kb"
End If
dgv1.Rows(ii).Cells(1).Value = f.Name
dgv1.Rows(ii).Cells(2).Value = (CInt(flen(f.FullName)) / 1024).ToString & " kb"
End If
Next
End Sub
Private Sub Form1_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
If IsNothing(th1) = False Then
th1.Abort()
End If
If IsNothing(th2) = False Then
th2.Abort()
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If IsNothing(th1) = False Then
th1.Abort()
End If
If IsNothing(th2) = False Then
th2.Abort()
End If
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
rep(Me)
End Sub
Private Sub Form1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown, Label2.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
dr = True
ax = MousePosition.X - Left
ay = MousePosition.Y - Top
End If
End Sub
Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove, Label2.MouseMove
If dr Then
Left = MousePosition.X - ax
Top = MousePosition.Y - ay
End If
End Sub
Private Sub Form1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp, Label2.MouseUp
dr = False
End Sub
Private Sub btnStop_Click(sender As System.Object, e As System.EventArgs) Handles btnStop.Click
astop = True
If IsNothing(th1) = False Then
th1.Abort()
End If
If IsNothing(th2) = False Then
th2.Abort()
End If
End Sub
Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
If Val(txtsize.Text) < 10 Then MsgBox("أقل رقم مقبول هو رقم عشرة ميجا بايت", MsgBoxStyle.Exclamation, "") : Exit Sub
th1 = New Threading.Thread(AddressOf makelist)
If th1.IsAlive = False Then
Dim s As New FolderBrowserDialog
If s.ShowDialog = Windows.Forms.DialogResult.OK Then
btnStart.Enabled = False
url = TextBox1.Text
folder = s.SelectedPath & "\"
' If File.Exists(path) = False Then File.Create(path).Close()
imax = Val(txtsize.Text) * mb
th1.Start()
End If
End If
Timer1.Start()
End Sub
End Class
تم تحرير المجلد و الملفات
وبقيت خطوة ايضا لاصلاح مشكلة طبعا هذا قبل الشروع في الفكرة الاساسية واللتي هي التحميل المتعدد لاني وقعت باشكالات منطقية.
اللهم إني أعوذ بك من غلبة الدين وغلبة العدو، اللهم إني أعوذ بك من جهد البلاء ومن درك الشقاء ومن سوء القضاء ومن شماتة الأعداء
اللهم اغفر لي خطيئتي وجهلي، وإسرافي في أمري وما أنت أعلم به مني، اللهم اغفر لي ما قدمت وما أخرت، وما أسررت وما أعلنت وما أنت أعلم به مني، أنت المقدم وأنت المؤخر وأنت على كل شيء قدير

