Prepending "\\?\" doesn't work for handling long paths
Asked Answered
A

1

4

I'm trying to find a workaround for the Windows character limitation that doesn't allow a file to be copied if its name is >= 260 characters. According to this MSDN article, if \\?\ is prepended to the file name, this will circumvent the filename length restriction.

I tried this test:

string source = "\\\\?\\C:\\Users\\xxxx\\Documents\\Visual Studio 2013\\Projects\\PDFConverterTester\\PDFConverterTester_BatchGUI\\bin\\Debug\\folder1\\a.txt";
string dest= "\\\\?\\C:\\Users\\xxxx\\Documents\\Visual Studio 2013\\Projects\\PDFConverterTester\\PDFConverterTester_BatchGUI\\bin\\Debug\\folder2\\a.txt";            
System.IO.File.Copy(source, dest);

But this threw an exception:

Illegal characters in path.

Should I be applying this prefix in a different way?

Edit: My company's IT policy will not allow me to install any new software without a lengthy review process, meaning that I can't update to 4.6.2. so I'm trying to figure out how to resolve this with Windows API calls from my existing .NET 4.5 installation of Visual Studio.

Antherozoid answered 27/12, 2016 at 0:29 Comment(1)
Are you running on .NET Framework 4.6.2?Droit
L
6

To enable long path support you either need to be targetting .Net Framework 4.6.2 (or newer) or you need to tell your app that it can support long filenames. To do that, add this to your app.config:

<runtime>
  <AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false" />
</runtime>

Further reading can be done here.

Lacework answered 27/12, 2016 at 0:43 Comment(4)
According to Jeremy Kuhne's blog on it, you need either, not both. If you target .NET Framework 4.6.2, then those switches already get set to false. You only need to explicitly set them to false if they were implicitly set to true, because you were targeting an older .NET Framework version. And that's also what the reference source indicates: see the if (version <= 40601) block.Chekhov
@hvd Funnily, I was just running through that now and deciding whether to update this answer.Lacework
Note that the blog post you link to in your answer is about transparently handling long file paths, i.e. without the application having to prepend \\?\. That's probably better but requires OS support (an up-to-date Windows 10 with non-standard settings). The one I linked to covers not rejecting \\?\ paths, requiring application workarounds for long paths but making it work on a lot more systems.Chekhov
My company's IT policy will not allow me to install any new software without a lengthy review process, meaning that I can't update to 4.6.2. so I'm trying to figure out how to resolve this with Windows API calls from my existing .NET 4.5 installation of Visual Studio. I'll update my question with this information.Antherozoid

© 2022 - 2024 — McMap. All rights reserved.