Windows10 中的最大文件名长度是多少? Java 会 try / catch 会通过 exeption 吗?

What is the maximum filename length in Windows 10? Java would try / catch would trough exeption?

在旧版本的 windows 中,我知道文件或文件夹的最大长度大约为 250 个字符,有人知道他们是否在 Windows 10 中更改了吗?

编辑:

我正在围绕给定的 API 编写一些 Java 代码来提取应用程序的一些内容,将有几千个文件,我有点担心可能会发生什么,因为我不知道文件名和路径会是什么样子,所以我会尝试防止这些错误,也许在保存之前更改 Java 中的名称(但最好保留原始名称),

所以你知道是否会有一些豁免吗? File file = new File(jsonFile); ?谢谢,我可能会抓住机会

如果你真的是指文件 name,我相信限制仍然是 "commonly" 255 个字符,请参阅引用的第三段 ("The Windows API has many...") 下面。

如果你指的是文件路径:你可以启用"Win32 long paths"选项。来自 this Microsoft document:

Maximum Path Length Limitation

In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters. A local path is structured in the following order: drive letter, colon, backslash, name components separated by backslashes, and a terminating null character. For example, the maximum path on drive D is "D:\some 256-character path string<NUL>" where "<NUL>" represents the invisible terminating null character for the current system codepage. (The characters < > are used here for visual clarity and cannot be part of a valid path string.)

Note

File I/O functions in the Windows API convert "/" to "\" as part of converting the name to an NT-style name, except when using the "\?\" prefix as detailed in the following sections.

Windows API 有许多函数也有 Unicode 版本以允许扩展长度路径,最大总路径长度为 32,767 个字符。这种类型的路径由反斜杠分隔的组件组成,每个组件最多为 GetVolumeInformation 函数的 lpMaximumComponentLength 参数中返回的值(此值通常为 255 个字符)。要指定扩展长度路径,请使用“\?\”前缀。例如,“\?\D:\very long path”。

Note

The maximum path of 32,767 characters is approximate, because the "\?\" prefix may be expanded to a longer string by the system at run time, and this expansion applies to the total length.

\?\”前缀也可以用于根据通用命名约定 (UNC) 构建的路径。要使用 UNC 指定此类路径,请使用“\?\UNC\”前缀。例如,“\?\UNC\server\share”,其中 "server" 是计算机的名称,"share" 是共享文件夹的名称。这些前缀不用作路径本身的一部分。它们表明路径应以最少的修改传递给系统,这意味着您不能使用正斜杠来表示路径分隔符,也不能使用句点来表示当前目录,也不能使用双点来表示父目录。因为您不能在相对路径中使用“\?\”前缀,所以相对路径的总长度始终限制为 MAX_PATH 个字符。

不需要对 Windows 文件 I/O API 函数使用的路径和文件名字符串执行任何 Unicode 规范化,因为文件系统处理路径和文件名作为 WCHAR 的不透明序列。您的应用程序需要的任何规范化都应考虑到这一点,在对相关 Windows 文件 I/O API 函数的任何调用之外执行。

使用API创建目录时,指定路径不能太长,不能追加8.3文件名(即目录名不能超过MAX_PATH 减 12).

shell和文件系统有不同的要求。可以使用 Windows API 创建路径,shell 用户界面无法正确解释。

在 Windows 10 版本 1607 及更高版本中启用长路径

从 Windows10 版本 1607 开始,MAX_PATH 限制已从常见的 Win32 文件和目录函数中删除。但是,您必须选择加入新行为。

要启用新的长路径行为,必须满足以下两个条件:

  • 注册表项 HKLM\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled(类型:REG_DWORD)必须存在并设置为 1。在首先调用受影响的 Win32 文件或目录函数(函数列表见下文)。在进程的生命周期内不会重新加载注册表项。为了让系统上的所有应用都能识别密钥的值,可能需要重新启动,因为某些进程可能在设置密钥之前已经启动。

Note

This registry key can also be controlled via Group Policy at Computer Configuration > Administrative Templates > System > Filesystem > Enable NTFS long paths.

  • 应用程序清单还必须包含 longPathAware 元素。

    <application xmlns="urn:schemas-microsoft-com:asm.v3">
        <windowsSettings xmlns:ws2="https://schemas.microsoft.com/SMI/2016/WindowsSettings">
            <ws2:longPathAware>true</ws2:longPathAware>
        </windowsSettings>
    </application>
    

如果您选择加入长路径行为,这些目录管理功能将不再具有 MAX_PATH 限制:CreateDirectoryW、CreateDirectoryExW GetCurrentDirectoryW RemoveDirectoryW SetCurrentDirectoryW。

如果您选择加入长路径行为,这些文件管理功能将不再具有 MAX_PATH 限制:CopyFileW、CopyFile2、CopyFileExW、CreateFileW、CreateFile2 , CreateHardLinkW, CreateSymbolicLinkW, DeleteFileW, FindFirstFileW, FindFirstFileExW, FindNextFileW, GetFileAttributesW, GetFileAttributesExW, SetFileAttributesW, GetFullPathNameW, GetLongPathNameW, MoveFileW, MoveFileExW, MoveFileWithProgressW, ReplaceFileW, SearchPathW, FindFirstFileNameW, FindNextFileNameW, FindFirstStreamW, FindGetNextFilnalPathW, 获取,CompressedFileSize0]

请注意,虽然那篇文章说组策略编辑器的设置是 "Enable NTFS long paths",但现在已经不是这样了;现在 "Enable Win32 long paths":

路径部分(目录或文件名)的最大长度通常为 255,但在外部存储(CD-ROM、FAT-16 软盘等)上的某些文件系统上可能更短。唯一确定的方法是调用 GetVolumeInformation。此函数将最大组件报告为 DWORD,但 Windows 当前限制为 255。

完整路径的最大长度变化:

人们常说的极限是260,它来自MAX_PATH常数。这是 Windows 95/98/ME 的绝对限制。

On Windows NT based systems this limit can be bypassed 当使用特殊前缀调用 kernel32 中低级文件系统函数的 Unicode 版本时。以 \?\ 为前缀的路径直接传递给 NT API。执行此操作时不支持相对路径。这些路径被限制在大约 32.000 个字符,这个限制来自 Windows 内核中使用的字符串格式。

即使这个方法已经支持了 25 年,但大多数 Windows 仍然仅限于 MAX_PATH。 shell/Explorer在很多地方都有这个限制,CreateProcess也有这个限制。

Windows 10 添加了对长度超过 MAX_PATH 且没有特殊前缀的路径的支持。仅当应用程序在其清单 中选择了此行为时才会激活此支持,并且 "Enable Win32 long paths" policy 已在系统上启用。

shell/Explorer 中的大部分 MAX_PATH 限制已在 Windows 10 中删除。