0

I'm looking for a way to avoid System.IO.PathTooLongException. I read that I could put \\?\ in front of a long path.

The problem is that I have FileInfo objects and I ask me how to add the \\?\ path prefix to my FileInfo paths without writting useless code.

Note that I have folders with several thousand of files to copy and I don't want that my application takes a lot of useless time during his process.

Thanks.

DirectoryInfo dir = new DirectoryInfo(sourceDirName);
FileInfo[] files = dir.GetFiles(Constants.ALL_FILES, SearchOption.AllDirectories);

foreach (FileInfo file in files)
{
    file.CopyTo(destAbsolutePath);
}
  • The last time I researched getting around the path limit I decided it was vastly easier to avoid paths that are too long than to re-write everything with native calls. [Here](http://stackoverflow.com/a/5188559/5095502) is some information. If you *only* want to be able to copy a file, then maybe it's worth it. Otherwise every single other call you pass that path into has to be modified and redirected through native win32 p/invoked stuff. – Quantic Aug 08 '16 at 16:47
  • I don't know about the `\\?\` feature, but if it works, I don't think you need to care about a performance issue when adding this to each single path. The I/O operations (copying files on a hard disk) will take much much more time than this simple string operation, you will never experience any difference. – René Vogt Aug 08 '16 at 16:47
  • Using `\\?\ ` is a hack and you should really avoid it if at all possible. Another (slightly more involved) option is to use .Net Core as that has had extensive internal surgery to work with extra long paths. – DavidG Aug 08 '16 at 16:57
  • https://blogs.msdn.microsoft.com/jeremykuhne/2016/07/30/net-4-6-2-and-long-paths-on-windows-10/ Meanwhile, use [a library](http://stackoverflow.com/a/14907544/17034). – Hans Passant Aug 08 '16 at 17:01
  • I'm on the framework 4.0 and the solution needs to run on Windows xp/vista/7/8/10 – Émile Pettersen-Coulombe Aug 08 '16 at 17:53

0 Answers0