Results 1 to 9 of 9

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

  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

  2. #2
    New Member
    Join Date
    Mar 2006
    Posts
    9

    Re: VB.NET : Show Copy Progress Dialog while copying Files

    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

  3. #3

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

    Re: VB.NET : Show Copy Progress Dialog while copying Files

    Quote Originally Posted by scandog
    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.

    I have added the Framework version to the thread title now.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

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

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    New Member
    Join Date
    Mar 2006
    Posts
    9

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

    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

  6. #6
    New Member
    Join Date
    Jul 2008
    Posts
    3

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

    Quote Originally Posted by 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.
    Thank you so much!!! This worked perfectly

    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)

  7. #7
    Lively Member therat324's Avatar
    Join Date
    Oct 2008
    Location
    Bethany, Oklahoma
    Posts
    94

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

    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

  8. #8
    New Member
    Join Date
    Jul 2008
    Posts
    3

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

    Quote Originally Posted by 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
    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

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

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

    Quote Originally Posted by 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
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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