Results 1 to 9 of 9

Thread: VB.NET (1.1) : Show Copy Progress Dialog while copying Files

Threaded View

  1. #1

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    VB.NET (1.1) : Show Copy Progress Dialog while copying Files

    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:
    1. Public Class FileCopy
    2.  
    3. #Region "API Declaration"
    4.     'Enum for holding Constants
    5.     Private Enum FO_Func As Short
    6.         FO_COPY = &H2
    7.         FO_DELETE = &H3
    8.         FO_MOVE = &H1
    9.         FO_RENAME = &H4
    10.         FOF_ALLOWUNDO = &H40
    11.         FOF_NOCONFIRMATION = &H10
    12.     End Enum
    13.     'Structure that will be used to pass values to the SHFileOPeration API
    14.     Private Structure SHFILEOPSTRUCT
    15.         Dim hwnd As Integer
    16.         Dim wFunc As Integer
    17.         Dim pFrom As String
    18.         Dim pTo As String
    19.         Dim fFlags As Short
    20.         Dim fAnyOperationsAborted As Boolean
    21.         Dim hNameMappings As Integer
    22.         Dim lpszProgressTitle As String
    23.     End Structure
    24.     'Declaration of the API
    25.     Private Declare Function SHFileOperation Lib "shell32.dll" Alias _
    26.          "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
    27. #End Region
    28.  
    29.  
    30.     'Copy Files function used to copy files from Source to Target
    31.     'Param1 : sSource --> The source file or Folder
    32.     'Param2 : sSource --> The target file or Folder
    33.     Public Shared Function CopyFiles(ByVal sSource As String, ByVal sTarget As String) As Boolean
    34.         Dim _ShFile As SHFILEOPSTRUCT
    35.         Try
    36.             _ShFile.wFunc = FO_Func.FO_COPY
    37.             _ShFile.fFlags = FO_Func.FOF_ALLOWUNDO
    38.             _ShFile.pFrom = sSource
    39.             _ShFile.pTo = sTarget
    40.             SHFileOperation(_ShFile)
    41.         Catch ex As Exception
    42.             MessageBox.Show(ex.Message)
    43.             Return False
    44.         End Try
    45.         Return True
    46.     End Function
    47. End Class

    In our code we can use it like this
    VB Code:
    1. 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.
    Last edited by Shuja Ali; Apr 10th, 2006 at 08:59 AM. Reason: Added the VB 2005 function
    Use [code] source code here[/code] tags when you post source code.

    My Articles

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