How to create a copy of a file having length more than 260 characters including file name using vb.net
When we are trying to create a copy using File.Copy method it throws exception as follows:
"The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters."
Is it possible, then please help...
You're running into the MAX_PATH limitation. As a work around you should be able to P/Invoke directly to kernel32.dll's CopyFile function and use the "\\?\" Prefix in front of the destination path to prevent hitting the MAX_PATH issue.
Note that while you're able to copy the file in the way most apps won't be able to open it since they are also limited by MAX_PATH.
A good overview of the problem can be found here: http://blogs.msdn.com/bclteam/archive/2007/02/13/long-paths-in-net-part-1-of-3-kim-hamilton.aspx
Some example code of P/Invoking into these methods using C# can be found in Part 2, here: http://blogs.msdn.com/bclteam/archive/2007/03/26/long-paths-in-net-part-2-of-3-long-path-workarounds-kim-hamilton.aspx
The library that schnaader linked to looks like it will save you the problem of P/Invoking into kernel32.dll, not sure if you want to take a dependency on an external dll or not.
There are tips for shortening the name ... see the section titled "Cause 4: Files exist in paths that are deeper than MAX_PATH characters" at http://support.microsoft.com/?kbid=320081#
You will find a complete library over here that supports long filenames.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With