How to cancel a file copy

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hdbbdh
    New Member
    • Dec 2008
    • 24

    #1

    How to cancel a file copy

    Hi,

    I am trying to make Backup program, that copy files from one location to another one, but I don't know how to allow user to cancel this operation.

    The code that I use is:
    Code:
    Public Class Form1
     
    Private Delegate Function CopyProgressRoutine(ByVal totalFileSize As Int64, ByVal totalBytesTransferred As Int64, ByVal streamSize As Int64, ByVal streamBytesTransferred As Int64, ByVal dwStreamNumber As Int32, ByVal dwCallbackReason As Int32, ByVal hSourceFile As Int32, ByVal hDestinationFile As Int32, ByVal lpData As Int32) As Int32
     
    Private Declare Auto Function CopyFileEx Lib "kernel32.dll" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal lpProgressRoutine As CopyProgressRoutine, ByVal lpData As Int32, ByVal lpBool As Int32, ByVal dwCopyFlags As Int32) As Int32
     
    Private Function CopyProgress(ByVal totalFileSize As Int64, ByVal totalBytesTransferred As Int64, ByVal streamSize As Int64, ByVal streamBytesTransferred As Int64, ByVal dwStreamNumber As Int32, ByVal dwCallbackReason As Int32, ByVal hSourceFile As Int32, ByVal hDestinationFile As Int32, ByVal lpData As Int32) As Int32
    ProgressBar1.Value = Convert.ToInt32(totalBytesTransferred / totalFileSize * 100)
    End Function
     
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim cpr As New CopyProgressRoutine(AddressOf CopyProgress)
    CopyFileEx("C:\myFile.dll", "D:\myFile.dll", cpr, 0, 0, 0)
    End Sub
     
    End Class
    Thank you
    Last edited by pbmods; Jan 11 '09, 08:55 PM. Reason: Added CODE tags.
  • raids51
    New Member
    • Nov 2007
    • 59

    #2
    you could use a filestream and a buffer instead, then copy it byte by byte. but this is slower then for example io.file.copy... . but you would have more control over the file, you could keep track of the progress, the speed that its being copied at and you could cancel or pause it.

    Comment

    • hdbbdh
      New Member
      • Dec 2008
      • 24

      #3
      Thank you raids51,
      Can you give me an example about that, because I am very fresh in programming.
      Thank you

      Comment

      • hdbbdh
        New Member
        • Dec 2008
        • 24

        #4
        Hi,

        I think I found the solution

        Code:
        Public Class Form1
            Private Delegate Function CopyProgressRoutine(ByVal totalFileSize As Int64, ByVal totalBytesTransferred As Int64, ByVal streamSize As Int64, ByVal streamBytesTransferred As Int64, ByVal dwStreamNumber As Int32, ByVal dwCallbackReason As Int32, ByVal hSourceFile As Int32, ByVal hDestinationFile As Int32, ByVal lpData As Int32) As Int32
            Private Declare Auto Function CopyFileEx Lib "kernel32.dll" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal lpProgressRoutine As CopyProgressRoutine, ByVal lpData As Int32, ByVal lpBool As Int32, ByVal dwCopyFlags As Int32) As Int32
         
            Private Const COPY_FILE_RESTARTABLE As Long = &H2
            Private Const PROGRESS_CONTINUE As Long = 0
            Private Const PROGRESS_CANCEL As Long = 1
            Dim blnCancel As Boolean = False
            Private Function CopyProgress(ByVal totalFileSize As Int64, ByVal totalBytesTransferred As Int64, ByVal streamSize As Int64, ByVal streamBytesTransferred As Int64, ByVal dwStreamNumber As Int32, ByVal dwCallbackReason As Int32, ByVal hSourceFile As Int32, ByVal hDestinationFile As Int32, ByVal lpData As Int32) As Int32
                ProgressBar1.Value = Convert.ToInt32(totalBytesTransferred / totalFileSize * 100)
                Me.Text = ProgressBar1.Value
                Application.DoEvents()
                If blnCancel = True Then
                    Return PROGRESS_CANCEL
                Else
                    Return PROGRESS_CONTINUE
                End If
            End Function
            Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                Dim cpr As New CopyProgressRoutine(AddressOf CopyProgress)
                CopyFileEx("C:\myFile.dll", "D:\myFile.dll", cpr, 0, blnCancel, 0)
            End Sub
            Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                blnCancel = True
            End Sub
        End Class
        Thank you

        Comment

        Working...