前面加上"\\?\"不适用于处理长路径 [英] Prepending "\\?\" doesn't work for handling long paths

查看:126
本文介绍了前面加上"\\?\"不适用于处理长路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找Windows字符限制的变通办法,如果文件名> = 260个字符,则不允许复制文件.根据

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.

我尝试了这个测试:

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);

但这引发了异常:

Illegal characters in path.

我应该以其他方式应用此前缀吗?

Should I be applying this prefix in a different way?

我公司的IT策略不允许我在没有冗长的审查过程的情况下安装任何新软件,这意味着我无法更新至4.6.2.因此,我试图找出如何通过现有的Visual Studio .NET 4.5安装中的Windows API调用解决此问题.

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.

推荐答案

要启用长路径支持,您需要定位.Net Framework 4.6.2(或更高版本),或者需要告诉您的应用程序它可以支持长路径文件名.为此,请将其添加到您的app.config:

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>

可以进一步阅读 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆