You are here : Blog
Search_Blog Minimize

KHSW Soft Blog Minimize
Aug 3

Written by: Ken Huysmans
2005-08-03T10:49:02 

Today I wanted to create a control that could copy files in VB.NET with some progress indication. I did some searches on the Internet and managed to build a working control. However, I would like to share the basics with all of you...

First declare some API functions:


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

Add a button (Button1) to your form and add this code to the Click event:

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:\Program Files\Common Files\Microsoft Shared\DAO\dao360.dll", "C:\dao360_delete.dll", cpr, 0, 0, 0)
End Sub

Make sure the file exists on your filesystem, I advise you to take a big file (> 100MB) to see the progressbar working...

Add a ProgressBar (ProgressBar1) to your form and create a function called CopyProgress:

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

Click the button and watch the spectacle ;-)


CopyFileEx will return a nonzero value in case of success, it will return 0 in case of error. Use this code to retrieve the error information:
Dim copyError As Exception = New System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error)

If you want to cancel the copy process and delete the new file, make sure CopyProgress returns PROGRESS_CANCEL
Private Const PROGRESS_CANCEL As Integer = 1

This function will overwrite an existing file by default. If you don't want this to happen, change the function call to
Private Const COPY_FILE_FAIL_IF_EXISTS As Integer = 1
CopyFileEx("C:\Program Files\Common Files\Microsoft Shared\DAO\dao360.dll", "C:\dao360_delete.dll", cpr, 0, 0, COPY_FILE_FAIL_IF_EXISTS)


Copying will fail if the file has the Hidden or the Readonly attribute. Use this code to reset the attributes to Normal:
Dim fi As New System.IO.FileInfo(destinationFileName)
If fi.Exists Then
fi.Attributes = IO.FileAttributes.Normal
End If

Tags:

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 


Copyright 2007 by KHSW Soft BVBA
Privacy StatementTerms Of Use