|
-
Jun 1st, 2006, 02:26 AM
#1
Thread Starter
New Member
[RESOLVED] [2005] API File operations error with multiple folders to copy
Hi,
I'm using a Filecopy class with an API declaration and the following function: Private Declare Function SHFileOperation Lib "shell32.dll" Alias _
"SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
I want to copy some files and folders using this function (it gives me the OS dialog box of the progress of the copy, .net functions for folder copy don't exist I have noticed). Several folders can be selected and are inserted in a listbox (as a string in an arraylist)
Clicking an a button makes the selected locations being copied in an iteration: Dim intI As Integer
Dim blnFinished As Boolean = False
For intI = 0 To strSource.Count - 1
lnFinished = CopyFiles( strsource(intI), strdest)
Next
The first filecopy execution succeeds most of the time but going further through the loop gives me following error for other filecopies: cannot read from source file or disk. Sometimes the first file operation fails and others based on the source strings kept in the arraylist don't. I 'm not sure what exactly the problem is but I'm guessing that I need a way to wait until the first filecopy has terminated before another call is made to the function (or a way to check if fileoperation is possible). When I only select one folder for copy there's no problem. Maybe I may not use the iteration to copy muliple folders but don't know how to make the application copy muliple folders by 1 user interaction (buton click event)
I've looked in the basics of threading but am not sure if this is a solution for my problem. I'm a beginner and would appreciate a lot if somebody could give me a hint or example to solve this issue since I've spent a lot of time already looking into this but didn't find what I was looking for (maybe the fact that I don't know what I'm exaclty looking for has got something to do with it )
If you need more info don't hesitate...
Thanks
Pascal
-
Jun 1st, 2006, 02:42 AM
#2
Re: [2005] API File operations error with multiple folders to copy
Welcome to the Forum. 
In 2005, you don't have to use SHFileOperation API to do that, Just checkout the My.Computer.FileSystem.CopyFile method. This method uses SHFileOperation API to copy files and does show you the progress dialog.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Jun 1st, 2006, 03:35 AM
#3
Thread Starter
New Member
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:
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
-
Jun 1st, 2006, 04:00 AM
#4
Re: [2005] API File operations error with multiple folders to copy
Does noone take any notice of Intellisense? CopyDirectory is overloaded and two of those overloads allow you to specify that a progress dialogue is displayed. The Intellisense tool tip would have told you that as you typed.
-
Jun 1st, 2006, 04:02 AM
#5
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:
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
-
Jun 1st, 2006, 04:33 AM
#6
Thread Starter
New Member
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?
-
Jun 1st, 2006, 04:39 AM
#7
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:
[i]My.Computer.FileSystem.CopyDirectory(sourceDirectoryName ,destinationDirectoryName ,overwrite)[/i]
And in my post I used this overload of the same method
VB Code:
[i]My.Computer.FileSystem.CopyDirectory(sourceDirectoryName ,destinationDirectoryName ,showUI)
[/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
-
Jun 1st, 2006, 05:18 AM
#8
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|