# VBForums CodeBank > CodeBank - Visual Basic .NET >  VB.NET (1.1) : Show Copy Progress Dialog while copying Files

## Shuja Ali

We have always wanted to have those progress bars that Windows Comes up with while copying/moving/deleting Files. In VB.NET we usually tend to use File.Copy function but this does not show us the progress of the File Copying. IN order to add this type of functionality to VB.NET application we can use SHFileOperation API. Here is a class that uses the SHFileOPeration API to copy files from one location to another
VB Code:
Public Class FileCopy
 #Region "API Declaration"
    'Enum for holding Constants 
    Private Enum FO_Func As Short
        FO_COPY = &H2
        FO_DELETE = &H3
        FO_MOVE = &H1
        FO_RENAME = &H4
        FOF_ALLOWUNDO = &H40
        FOF_NOCONFIRMATION = &H10
    End Enum
    'Structure that will be used to pass values to the SHFileOPeration API
    Private Structure SHFILEOPSTRUCT
        Dim hwnd As Integer
        Dim wFunc As Integer
        Dim pFrom As String
        Dim pTo As String
        Dim fFlags As Short
        Dim fAnyOperationsAborted As Boolean
        Dim hNameMappings As Integer
        Dim lpszProgressTitle As String
    End Structure
    'Declaration of the API
    Private Declare Function SHFileOperation Lib "shell32.dll" Alias _
         "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
#End Region
      'Copy Files function used to copy files from Source to Target
    'Param1 : sSource --> The source file or Folder
    'Param2 : sSource --> The target file or Folder
    Public Shared Function CopyFiles(ByVal sSource As String, ByVal sTarget As String) As Boolean
        Dim _ShFile As SHFILEOPSTRUCT
        Try
            _ShFile.wFunc = FO_Func.FO_COPY
            _ShFile.fFlags = FO_Func.FOF_ALLOWUNDO
            _ShFile.pFrom = sSource
            _ShFile.pTo = sTarget
            SHFileOperation(_ShFile)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        End Try
        Return True
    End Function
End Class

In our code we can use it like this 
VB Code:
FileCopy.CopyFiles("C:\*.*", "C:\NewFolder")

In VB 2005 Express, almost all the Shell32 function have been included in MY object.

Edit--
The above solution is for 2003 version. If you are using VB 2005 then you don't have to call this API. Almost all the FileSystem operations cane be performed by My.Computer.FileSystem Object.

----------


## scandog

Hi 

I have tried using this script and everthing seem to be working fine.  I have just updated to VS 2005 pro.  Now the script wont work.  (2005 converted my program for use in 2005)

When I run it I get the following message.  cannot copy file: Cannot read from the source file or disk.

I can load the same program into VS 2003 and it works just fine.

The error seems to happen after the class runs.  It returns true after calling the class.  I can't capture this error either.  

Can anyone help me.

I have run it on my main computer and my laptop and they both have this problem but on another computer it works just fine.

Thanks

----------


## Shuja Ali

> I have tried using this script and everthing seem to be working fine.  I have just updated to VS 2005 pro.  Now the script wont work.  (2005 converted my program for use in 2005)


 I have not tested this in VB 2005[/quote] I have not tested this in VB 2005. I have not yet started working on 2005.   :Mad:  

I have added the Framework version to the thread title now.

----------


## jmcilhinney

It's not required in VB 2005, as you alluded to at the bottom of your original post.  My.Computer.FileSystem.CopyFile and .CopyDirectory allow you to display a progress dialogue.

----------


## scandog

That worked perfectly, the only thing it didn't do was allow for *.* at least I couldn't get it to work.

I ended up reading the directory and placing all the filenames in an array and then looping through the array and copying each file.

Just so you know the script above has a warning in it in vb 2005

Variable '_ShFile' is passsed by reference before it has been assigned a value.  A null refernece exception could result at runtime.  Make sure the structure or all the reference members are initalized before use.

Don't know if that might cause a problem.

Seemed very strange.  I was able to get it to work if the source was my g: drive and on some directories on the c: drive but not consitantly

Thanks for the help I am new to VB

----------


## Paleogirl2003

> It's not required in VB 2005, as you alluded to at the bottom of your original post.  My.Computer.FileSystem.CopyFile and .CopyDirectory allow you to display a progress dialogue.


Thank you so much!!! This worked perfectly  :Smilie: 

This has several overrides, this being the one I chose:
My.Computer.FileSystem.CopyFile(SourceFileName As String,  DestinationFileName As String, ShowUi as Microsoft.VisualBasic.FileIO.UIOption)

My example:
My.Computer.FileSystem.CopyFile(FileToCopy, NewFileName, FileIO.UIOption.AllDialogs)

----------


## therat324

Is there a restriction on how big a file you can move? It lets me move anything untill I get up to about 18 mb

----------


## Paleogirl2003

> Is there a restriction on how big a file you can move? It lets me move anything untill I get up to about 18 mb


Hi Therat324,

I do not believe there are size restrictions as the file I copy is 41 mb.
Maybe there isn't enough room on your destination drive?

~Kelly

----------


## jmcilhinney

> Is there a restriction on how big a file you can move? It lets me move anything untill I get up to about 18 mb


What exactly does it mean to say that it won't let you move a file over that size?  Does the computer just say "sorry, can't let you do that", or maybe something specific actually happens, like an exception is thrown?  If you tell us what actually happens then we might be able to tell you why.

----------

