Results 1 to 20 of 20

Thread: [RESOLVED] Multiple file move via DragAndDrop

Hybrid View

  1. #1
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Multiple file move via DragAndDrop

    You're not having problems getting your form to process multiple dragged files right? What you want is an accurate multifile copy progress bar. In that case, you'll want manual copying. Use filestreams with a progressbar. Something like this:

    Code:
    Public Class Form1
        Friend WithEvents bgCopy As New System.ComponentModel.BackgroundWorker With {.WorkerReportsProgress = True}
    
        Private Structure copyDetails
            Dim moveFiles As Boolean
            Dim destination As String
            Dim filesToCopy() As IO.FileInfo
        End Structure
    
        Private Sub Button1_Click( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs _
        ) Handles Button1.Click
            Dim files(ListBox1.Items.Count - 1) As IO.FileInfo
            Dim total As Long = 0
    
            For i As Integer = 0 To ListBox1.Items.Count - 1
                files(i) = New IO.FileInfo(ListBox1.Items(i).ToString())
                total += files(i).Length
            Next
    
            ProgressBar1.Maximum = total
            ProgressBar1.Step = 4096
    
            bgCopy.RunWorkerAsync( _
                New copyDetails() With { _
                    .destination = TextBox1.Text, _
                    .filesToCopy = files, _
                    .moveFiles = CheckBox1.Checked _
                } _
            )
        End Sub
        Private Sub bgCopy_DoWork( _
            ByVal sender As System.Object, _
            ByVal e As System.ComponentModel.DoWorkEventArgs _
        ) Handles bgCopy.DoWork
            Dim copyArgs As copyDetails = DirectCast(e.Argument, copyDetails)
            Dim bgWorker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
            For Each f As IO.FileInfo In copyArgs.filesToCopy
                Using fsIn As New IO.FileStream( _
                    f.FullName, IO.FileMode.Open, IO.FileAccess.Read _
                )
                    Using fsOut As New IO.FileStream( _
                        f.FullName.Replace(f.DirectoryName, copyArgs.destination), IO.FileMode.Create, IO.FileAccess.Write _
                    )
                        Dim offset As Long = 0
                        Dim total As Long = fsIn.Length
                        Dim toCopy(4096) As Byte
    
                        While offset + 4096 < total
                            fsIn.Read(toCopy, 0, toCopy.Length)
                            fsOut.Write(toCopy, 0, toCopy.Length)
    
                            bgWorker.ReportProgress(0)
                            offset += 4096
                        End While
    
                        fsIn.Read(toCopy, 0, toCopy.Length)
                        fsOut.Write(toCopy, 0, CInt(total - offset))
    
                        bgWorker.ReportProgress(0)
    
                        fsIn.Flush()
                        fsOut.Flush()
                    End Using
                End Using
    
                If copyArgs.moveFiles Then _
                    f.Delete()
            Next
        End Sub
    
        Private Sub bgCopy_ProgressChanged( _
            ByVal sender As System.Object, _
            ByVal e As System.ComponentModel.ProgressChangedEventArgs _
        ) Handles bgCopy.ProgressChanged
            If ProgressBar1.Value < ProgressBar1.Maximum - ProgressBar1.Step Then
                ProgressBar1.PerformStep()
            Else
                ProgressBar1.Value = ProgressBar1.Maximum
            End If
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Using f As New OpenFileDialog() With {.Multiselect = True}
                f.ShowDialog()
                ListBox1.Items.AddRange(f.FileNames)
            End Using
        End Sub
    End Class
    This may not be the most efficient method, we'll have to get input from others. I used a background worker for obvious reasons.
    Last edited by MaximilianMayrhofer; Jun 30th, 2009 at 01:14 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width