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 anotherVB 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 thisVB 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.




Reply With Quote