Results 1 to 8 of 8

Thread: [RESOLVED] [2005] API File operations error with multiple folders to copy

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    9

    Re: [2005] API File operations error with multiple folders to copy

    Thanks for your quick reply and friendly welcome greeting!
    I used: My.Computer.FileSystem.CopyDirectory(strSource.Item(intI), strDest, True), in stead since I want to copy directories what i already tested before...
    This only copies the contents of the folders and does it 'hidden' , no process dialog.
    It solves the error I had since this method copies several folders (though it doesn't create the root folder in the destination which is also needed) but I really need the progress to be visible
    This is the class I'm using:
    VB Code:
    1. FileCopy Class
    Public Class FileCopy
    #Region "API Declaration"
    'Enum for holding Constants
    Private Enum FO_Func As Short
    ' Operations available
    FO_COPY = &H2 ' Copy File/Folder
    FO_DELETE = &H3 ' Delete File/Folder
    FO_MOVE = &H1 ' Move File/Folder
    FO_RENAME = &H4 ' Rename File/Folder
    ' Flags available
    FOF_ALLOWUNDO = &H40 ' Allow to undo rename, delete ie sends to recycle bin
    FOF_NOCONFIRMATION = &H10 ' No File Delete or Overwrite Confirmation Dialog
    FOF_FILESONLY = &H80 ' Only allow files
    FOF_SILENT = &H4 ' No copy/move dialog
    FOF_SIMPLEPROGRESS = &H100 ' Does not display file names
    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 = &H2 ' = 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

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: [2005] API File operations error with multiple folders to copy

    Here is the code that will display the progress bar while copying
    VB Code:
    1. My.Computer.FileSystem.CopyDirectory("C:\Source", "C:\Destination", FileIO.UIOption.AllDialogs)

    And remember you should try to make things simple rather than making them complex. When there is a simple method that does exactly same things as SHFileOPeration API does then you should be using that simple method.

    It is always better to use VBCODE tags when posting code, it makes your post more readable.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    9

    Re: [2005] API File operations error with multiple folders to copy

    Thanks for the tips!
    It is working fine now... KISS Keep It Stupid Simple - I'll try to keep this in mind
    It's all about knowing what's possible - understanding the classes I suppose
    Any idea why I got the error message? (to learn from) I suppose API's are all covered in the framework and aren't used anymore?

  4. #4
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: [2005] API File operations error with multiple folders to copy

    Were you getting an Error Message? The reason why it was not showing the progress dialog was that you were using a different overload of the method. You were using this overload
    VB Code:
    1. [i]My.Computer.FileSystem.CopyDirectory(sourceDirectoryName ,destinationDirectoryName ,overwrite)[/i]
    And in my post I used this overload of the same method
    VB Code:
    1. [i]My.Computer.FileSystem.CopyDirectory(sourceDirectoryName ,destinationDirectoryName ,showUI)
    2. [/i]

    Yes you should try to go through MSDN before you start on a new class. It is always helpful. And most of the Shell32 functions have been included in My.Computer.FileSystem class, so you don't have to use APIs anymore. However that doesn't mean that all the APIs have been included in the Framework.

    KISS - Keep It Short and Simple (and not stupid)

    Also, if your problem has been resolved, please mark your thread resolved by opening thread tools at the top of this thread and select Mark Thread resolved.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    9

    Re: [RESOLVED] [2005] API File operations error with multiple folders to copy

    I didn't get an error with your solution but was getting one before using the API class: cannot read from source file or disk; sometimes in the first iteration, sometimes at the end or in the middle. I supposed it was because FileOperation was in use but I'm not sure.
    But the problem is solved and the tread is set to resolved
    Thanks a lot!

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