kernel32 CopyFile from VB
-
Monday, August 13, 2007 1:54 PM
I am trying to use the kernel32 CopyFile method to copy some files that throw a PathTooLongException. The code looks like this:
Imports System
Imports System.Runtime.InteropServices
Imports System.IOPublic Class frmFileCopy
<DllImport("kernel32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=False)> _
Public Shared Function CopyFile(ByVal lpExistingFilename As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Boolean) As Boolean
End FunctionPrivate Sub CopyLongFile(ByVal fileName As String, ByVal strDestPath As String)
Try
Dim boolResult As Boolean
Dim formattedName As String = "\\?\" + fileName
Dim formattedNewName As String = "\\?\" + strDestPath
boolResult = CopyFile(formattedName, formattedNewName, False)
Debug.WriteLine("Result: " & boolResult.ToString)Catch ex As Exception
Throw ex
End TryEnd Sub
End ClassIt looks correct but the files are not copied. Any ideas?
thanks,
Luis
All Replies
-
Monday, August 13, 2007 2:46 PMModerator
What do your paths look like?
Are you actually copying files that's path is over 248 characters?
-
Monday, August 13, 2007 3:10 PM
Here's what the path's look like
\\Server2\users\brewster\profile\Application Data\Macromedia\Flash Player\#SharedObjects(3)\UHBESKBD(2)\l.yimg(2).com\cosmos.bcst.yahoo(2).com\ver(2)\225(2)\embed-2007-04-06-1213(2)\swf(2)\yup_embed_module(2).swf\TestMovie_Config_Info.sol
C:\F1BackupDebug\Full_08-10-2007-16-59\erver2\users\brewster\profile\Application Data\Macromedia\Flash Player\#SharedObjects(3)\UHBESKBD(2)\l.yimg(2).com\cosmos.bcst.yahoo(2).com\ver(2)\225(2)\embed-2007-04-06-1213(2)\swf(2)\yup_embed_module(2).swf\TestMovie_Config_Info.sol
Luis
-
Monday, August 13, 2007 3:21 PMModerator
Yeah those certainly are long file names...
perhaps the problem is that the CopyFile API function is still calling an ANSI version. The MSDN documentation states the function will use the ANSI version which is limited to a max characters value. If you want to extend the path length to be up to 32,767 characters, you need to call the unicode version which is CopyFileW. Specifying an entry point to the specific function you want may work.
Try this declaration instead:
Code Snippet<DllImport(
"kernel32.dll", EntryPoint:="CopyFileW", CharSet:=CharSet.Unicode, ExactSpelling:=False)> _ Public Shared Function CopyFile(ByVal lpExistingFilename As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Boolean) As Boolean End Function -
Monday, August 13, 2007 4:38 PM
Thanks. This function works. However, when I right-click on the copied files I only get 2 menu options Open With and Send To. I don't get the standard file options (copy, delete, etc...). I imagine that this is because the path is too long.
Also, if I check the properties on the folder that contains the files the amount of files listed is wrong.
-
Monday, August 13, 2007 5:25 PMModerator
what happens when you right click on the source files? do you get the same truncated menu? It could be that Windows Explorer is not using unicode versions of functions dealing with the file paths, so when explorer needs to compile the menu to show when you right click, some of those items fail and the menu items dont get created.
A simple test would be to copy the files to a local "short" directory and then confirm that the explorer menus work fine there. Then you will know for sure its related to the length of the path.
-
Monday, August 13, 2007 5:30 PMWhen I right click the source file the file menu works correctly. They also work correctly if I copy the file to a directory with a shorter path.
-
Monday, August 13, 2007 5:32 PMModerator
Sounds like the path is doing it then.
Not sure if it is an option for you, but you could always map a network drive letter to the path you are accessing. This would make the path a whole lot shorter and avoid any of these issues.

