Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a copy of a file having length more than 260 characters

Tags:

excel

vb.net

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...

like image 660
Suman Avatar asked Jan 16 '09 06:01

Suman


3 Answers

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.

like image 85
Matt Ellis Avatar answered Sep 28 '22 14:09

Matt Ellis


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#

like image 28
ChrisW Avatar answered Sep 28 '22 15:09

ChrisW


You will find a complete library over here that supports long filenames.

like image 30
jdelimon Avatar answered Sep 28 '22 14:09

jdelimon