How to create a copy of a file longer than 260 characters

How to create a copy of a file longer than 260 characters including a file name using vb.net

When we try to create a copy using the File.Copy method, it throws an exception as follows:

"The specified path, file name, or both are too long. The full file name must be less than 260 characters, and the directory name must be less than 248 characters."

Is this possible, then please help ...

+6
excel
source share
7 answers

You are using the MAX_PATH constraint. During operation, you should be able to P / Invoke directly access the CopyFile kernel32.dll function and use the prefix "\\? \" In front of the destination path to prevent the problem with MAX_PATH.

Please note that although you can copy the file in such a way that most applications will not be able to open it, as 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 examples of P / Invoking code in 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 in which schnaader is linked looks like this will save you from the P / Invoking problem in kernel32.dll, not sure if you want to depend on an external dll or not.

+9
source share

There are tips for shortening the name ... see the section entitled " Reason 4: files exist in paths deeper than MAX_PATH characters ) at http://support.microsoft.com/?kbid=320081#

+2
source share

Here you will find the complete library that supports long file names.

+2
source share

I have a solution to this problem.

In R&D, I found that we can rename the specified path directory, even if the total length exceeds 260 characters, and then we can copy the file from the specified location to a new (temporary) location for our scanning purpose. And finally, we can again rename the path to the original file.

+1
source share

What I usually do in this case:

1) Count the file name length

2) If FileNameLength > 259 , then trim the file name enough to process the new name, and then copy the file with the trimmed destination name.

0
source share
0
source share

jdelimon was already pointing to the Delimon.Win32.IO library, but the last time they were updated was 2012 (.Net 2.0). Here is another library: AlphaFS

They climb to .Net 4.5.2 (29/6/2017). I used the very short #C program, which will be used as a COM add-in in Excel. See this link for how to create a program and this link for copying a program . You only need to assign the .NET 4.5 Program, add a link to AlphaFS.dll for 4.5, and replace using System.IO with using Alphaleonis.Win32.Filesystem; .

0
source share

All Articles