[英]Why can't .NET detect directories with long file paths?
I can't enumerate the contained files in a directory if the full path to those files exceeds 260 chars. 如果这些文件的完整路径超过260个字符,则无法枚举目录中包含的文件。 The following code shows the problem:
以下代码显示了该问题:
void TestLongPath(DirectoryInfo testDirectory)
{
if (testDirectory.Exists)
{
try
{
testDirectory.GetFiles("SomeFileNamePattern*");
}
catch (System.IO.DirectoryNotFoundException)
{
Console.WriteLine("Long path test failed for " + testDirectory.FullName);
}
}
}
My app.manifest file contains: 我的app.manifest文件包含:
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>
But all that did was change the error from PathTooLongException to DirectoryNotFoundException. 但是所有要做的就是将错误从PathTooLongException更改为DirectoryNotFoundException。
Here's my App.config: 这是我的App.config:
<?xml version="1.0" encoding="utf-8"?><configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
<runtime>
<AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false" />
</runtime></configuration>
I'm on Windows 10 Pro, using Visual Studio 2019 16.1.1. 我在Windows 10 Pro上,使用Visual Studio 2019 16.1.1。 I'm targeting .NET 4.7.2.
我的目标是.NET 4.7.2。
How can I enumerate files in these overlong directories? 如何枚举这些超长目录中的文件? They're on a shared network drive that I don't have control over, so renaming the directories is not an option for me.
它们位于我无法控制的共享网络驱动器上,因此重命名目录对我来说不是一种选择。
I've got it solved using information from here: What is the maximum amount of characters or length for a Directory? 我已经使用这里的信息解决了它:目录的最大字符数或长度是多少?
For .NET 4.6.2 or later: "Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false" in App.Config has no effect. 对于.NET 4.6.2或更高版本:App.Config中的“ Switch.System.IO.UseLegacyPathHandling = false; Switch.System.IO.BlockLongPaths = false”无效。
You don't need to prefix paths with \\\\?\\ 您无需在路径前加上\\\\?\\
You DO need the registry key HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\FileSystem\\LongPathsEnabled set to 1. 您确实需要将注册表项HKEY_LOCAL_MACHINE \\ SYSTEM \\ CurrentControlSet \\ Control \\ FileSystem \\ LongPathsEnabled设置为1。
And you DO need 而你确实需要
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>
in your app.manifest file. 在您的app.manifest文件中。
This code always seems to return 260, even when long paths are enabled: 即使启用了长路径,此代码似乎总是返回260:
FieldInfo maxPathField = typeof(Path).GetField("MaxPath",
BindingFlags.Static |
BindingFlags.GetField |
BindingFlags.NonPublic);
int maxPathLength = (int)maxPathField.GetValue(null);
Console.WriteLine("Max path length is " + maxPathLength);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.