如何处理与超过259个字符的名称的文件吗? [英] How to deal with files with a name longer than 259 characters?

查看:1573
本文介绍了如何处理与超过259个字符的名称的文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在上走过一些目录中的所有文件并执行这些文件中的某些操作的应用程序。其中,我必须检索文件的大小,当该文件的修改日期。

I'm working on an application which walks through every file in some directories and does some actions with those files. Among others, I must retrieve the file size and the date when this file was modified.

某些文件的全名(目录+文件名)太长,我无法使用.NET框架的FileInfo ,这是仅限于 MAX_PATH (260个字符)。许多网络资源劝到P使用原生Win32函数/调用来访问他们的名字都太长的文件。

Some file full names (directory + file name) being too long, I couldn't use .NET Framework FileInfo, which is limited to MAX_PATH (260 characters). Many web sources advised to use native Win32 functions through P/Invoke to access the files whose names are too long.

目前,完全相同的问题似乎与Win32函数产生。例如, GetFileAttributesEx (KERNEL32.DLL)失败,Win32错误3 ERROR_PATH_NOT_FOUND为270个字节的路径。

Currently, the exactly same problem seems to arise with Win32 functions. For example, GetFileAttributesEx (kernel32.dll) fails with Win32 error 3 ERROR_PATH_NOT_FOUND for the path of 270 bytes.

非常相同的文件可以成功地从的Notepad2打开并成功地与Windows资源管理器中显示(但如Visual Studio 2010中无法打开它,因为259个字符的limit¹)。

The very same file can successfully be opened from Notepad2 and successfully displayed with Windows Explorer (but Visual Studio 2010 for example fails to open it because of the 259 characters limit¹).

我能做些什么才能够访问一个文件时,文件路径是长270个字符?

What can I do to be able to access a file when the file path is 270 characters long?

注:


  • 删除或文件路径长度超过259个字符的文件忽略不是一个解决方案。

  • Removing or ignoring files with the file path length longer than 259 characters is not a solution.

我在寻找统一code兼容的唯一的解决办法。

I'm looking for Unicode-compatible solutions only.

该应用程序将在Windows 2008 / Vista的下运行或更高版本的.NET Framework 4的安装。

The application will run under Windows 2008/Vista or later with .NET Framework 4 installed.

¹出人意料的是,微软Word 2007中失败,抱怨说软盘太小,这没有任何软盘驱动器,或者说RAM内存不足时,有4 GB的计算机上RAM左,终于说:杀毒软件[...]需要更新。他们将停产一天在这样的关键产品显示至少这样愚蠢的错误毫无意义微软的Office?

推荐答案

使用 \\\\。\\ C:\\?Verrrrrrrrrrrry长径 语法和P /调用Win32 API函数的UNI code版本。
命名文件,路径和命名空间

在Windows API有许多功能也有统一code版本以允许扩展长度路径的32,767个字符的最大路径总长度。这种类型的路径是由由反斜杠分离元件,每个可达(这个值通常是255个字符)在GetVolumeInformation函数的lpMaximumComponentLength参数返回的值。的要指定扩展长度路径,使用 \\\\?\\ preFIX。例如, \\\\ \\ D:\\很长的路

The Windows API has many functions that also have Unicode versions to permit an extended-length path for a maximum total path length of 32,767 characters. This type of path is composed of components separated by backslashes, each up to the value returned in the lpMaximumComponentLength parameter of the GetVolumeInformation function (this value is commonly 255 characters). To specify an extended-length path, use the \\?\ prefix. For example, \\?\D:\very long path.

Microsoft支持页面也可能是有趣的。

在<一个非常广泛的解释href=\"http://blogs.msdn.com/b/bclteam/archive/2007/02/13/long-paths-in-net-part-1-of-3-kim-hamilton.aspx\">Long在.NET中通过的路径汉密尔顿金在BCL团队博客列出了在处理这些路径他声称几个提高警惕此语法仍然没有.NET支持的直接原因:

A very extensive explanation in Long Paths in .NET by Kim Hamilton at the BCL Team blog lists a few hitches in handling these paths which he claims are the reason this syntax is still not supported in .NET directly:

有我们不愿在过去增加长路径几个原因,为什么我们还是小心它&LT; ...>

There are several reasons we were reluctant to add long paths in the past, and why we’re still careful about it <...>.

&LT; ...>的 \\\\。\\ preFIX不仅使长路径?;它会导致路径被传递到文件系统与由Windows API的最小修改。结果是, \\\\?\\ 关闭由Windows API的执行,包括删除尾随空格,扩大文件名归'。'和'..',转换相对路径为完整路径,所以针对&lt; ...>

<...> the \\?\ prefix not only enables long paths; it causes the path to be passed to the file system with minimal modification by the Windows APIs. A consequence is that \\?\ turns off file name normalization performed by Windows APIs, including removing trailing spaces, expanding ‘.’ and ‘..’, converting relative paths into full paths, and so on.<...>

&LT; ...>与 \\\\。\\ preFIX可以在大多数的文件相关的的Windows API,但并非所有的Windows API。例如,调用LoadLibrary&LT; ...>如果文件名是超过MAX_PATH再失败。 &LT; ...>还有整个的Windows API类似的例子;一些解决方法存在,但他们是在逐案的基础。

<...> Long paths with the \\?\ prefix can be used in most of the file-related Windows APIs, but not all Windows APIs. For example, LoadLibrary<...> fails if the file name is longer than MAX_PATH. <...> There are similar examples throughout the Windows APIs; some workarounds exist, but they are on a case-by-case basis.

另一个因素&LT; ...>与其他基于Windows的应用程序和Windows shell本身&LT兼容性; ...>

Another factor <...> is compatibility with other Windows-based applications and the Windows shell itself <...>

由于这个问题正变得越来越常见&LT; ...>有力度整个微软解决这一问题。事实上,作为一个及时的Vista的插件,你会发现一对夫妇的减少击中MAX_PATH限制的机会的变化:许多特殊的文件夹名称已缩短,更有趣的是,外壳使用自动路径功能萎缩&LT; ...>试图将它们挤入260个字符

Because this problem is becoming increasingly common <...> there are efforts throughout Microsoft to address it. In fact, as a timely Vista plug, you’ll notice a couple of changes that reduce the chance of hitting the MAX_PATH limit: many of the special folder names have shortened and, more interestingly, the shell is using an auto-path shrinking feature <...> to attempt to squeeze them into 260 characters.

警告:您可能需要直接调用的Windows API,因为我认为在.NET Framework可能不支持这种路径语法


Warning: You might need to call the Windows APIs directly, since I think the .NET Framework might not support this kind of path syntax.

这篇关于如何处理与超过259个字符的名称的文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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