简体   繁体   中英

Prepending “\\?\” doesn't work for handling long paths

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM