有什么方法可以解决 FileSystemInfo.Fullname 有时抛出的 PathTooLongException 吗?

发布于 2024-12-01 09:06:40 字数 744 浏览 3 评论 0原文

当我访问 FileSystemInfo 对象的 Fullname 属性时,我的硬盘驱动器上有一些文件抛出 PathTooLongException 。有什么办法可以解决这个问题(不包括重命名文件,这不是一个选项)?

http://msdn.microsoft.com/其他答案提到的 en-us/library/aa365247%28VS.85%29.aspx#maxpath 建议在文件名,但在本例中,DirectoryInfo.GetFileSystemInfos() 负责创建 FileSystemInfo 对象,而 DirectoryInfo 不接受该前缀,因此有没办法用它。

答案“ PathTooLongException in C# code ”没有帮助,因为这是一个多线程应用程序,我无法不断更改当前的应用程序路径。

我真的必须使用 PInvoke 执行所有操作才能读取硬盘上的每个文件吗?

I have files on my hard drive that throw a PathTooLongException when I access the Fullname property of a FileSystemInfo object. Is there any way around this (excluding renaming the files which is not an option)?

http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx#maxpath mentioned by other answers suggests putting a "\?\" prefix on the file name but in this case the DirectoryInfo.GetFileSystemInfos() is responsible for creating the FileSystemInfo objects and DirectoryInfo doesn't accept that prefix so there's no way to use it.

The answer " PathTooLongException in C# code " doesn't help because this is a multi-threaded application and I can't keep changing the current application path.

Do I really have to do everything with PInvoke just to be able to read every file on the hard drive?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

网白 2024-12-08 09:06:40

从 Windows 10(或 Windows Server 2016)和 .Net 4.6.2 开始,如果打开注册表设置,则可以直接支持长路径,并且您的应用程序被标记为“长路径感知”。

可以通过计算机配置下的本地组策略编辑器 (gpedit.msc) 访问该设置。 管理模板 > 所有设置 > 启用 Win32 长路径

为了将您的应用程序标记为“长路径感知”,请将此部分添加到您的清单文件中:

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

此外,如果您的应用程序面向早于4.6.2,您需要在 App.config 文件中添加一个部分:

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

有关详细信息,请参阅:

https://blogs.msdn.microsoft.com/jeremykuhne/2016/07/30/net-4-6-2-and-long-paths-on-windows-10/
https://msdn.microsoft.com/en- us/library/aa365247(v=vs.85).aspx

(据我所知,这只会影响基本的 Windows 文件系统 API。非文件系统 API 可能仍然会限制为 260 个字符)

As of Windows 10 (or Windows Server 2016) and .Net 4.6.2, long paths can be supported directly if a registry setting is turned on, and your application is marked as being "long path aware".

The setting can be accessed via the Local Group Policy Editor (gpedit.msc), under Computer Configuration > Administrative Templates > All Settings > Enable Win32 long paths

In order to mark your application as "long path aware", add this section to your manifest file:

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

Additionally, if your application targets a version of the .Net framework earlier than 4.6.2, you'll need to add a section to your App.config file:

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

For more information see:

https://blogs.msdn.microsoft.com/jeremykuhne/2016/07/30/net-4-6-2-and-long-paths-on-windows-10/
https://msdn.microsoft.com/en-us/library/aa365247(v=vs.85).aspx

(As far as I know, this only affects the basic Windows filesystem APIs. Non-filesystem APIs may still be limited to 260 characters)

迷鸟归林 2024-12-08 09:06:40

这看起来很有趣...... Codeplex 长路径包装器

长路径包装器提供了使其成为可能的功能更容易使用超过 System.IO 命名空间当前 259 个字符限制的路径。使用长路径类,项目现在可以使用最多 32,000 个字符的路径。

我会尝试一下,但我立即注意到它没有提供与 DirectoryInfo.GetFileSystemInfos() 等效的方法,因此需要进行一些修改。

This looks interesting ... Codeplex Long Path Wrapper

The long path wrapper provides functionality to make it easier to work with paths that are longer than the current 259 character limit of the System.IO namespace. Using the long path classes, projects can now use paths up to 32,000 characters.

I'll give that a try, though I note immediately it doesn't provide an equivalent method to DirectoryInfo.GetFileSystemInfos() so it's going to need some modification.

佼人 2024-12-08 09:06:40

能够在超过 259 个字符的路径中生存的程序并不多。对于 winapi 层来说这是相当严格的限制,MAX_PATH 无处不在。它已被考虑用于.NET,但没有具体结果。博客文章系列在此结束,并在底部提供指向先前条目的链接。

There are not many programs that can survive a path larger than 259 characters. Pretty hard limit for the winapi layer, MAX_PATH is everywhere. It has been considered for .NET but without concrete results. Blog post series ends here with links to previous entries at the bottom.

谈情不如逗狗 2024-12-08 09:06:40

正确处理长路径并不困难 - 例如 SetACL 就可以做到。但是:

  • .NET 框架类不支持长路径,因此您无法使用它们,
  • 您需要为每个文件系统 API 函数编写一个包装器,以便它为本地和 UNC 路径使用正确的长路径

以下是 MSDN 上的文档:长路径:http://msdn.microsoft.com/en-us/library/aa365247(v=vs.85).aspx

Correctly working with long paths is not that difficult - SetACL does it, for example. But:

  • the .NET framework classes do not support long paths so you cannot use them
  • you need to write a wrapper for each file system API function so that it uses the correct long path both for local and UNC paths

Here is the documentation on MSDN about long paths: http://msdn.microsoft.com/en-us/library/aa365247(v=vs.85).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文