Ebo*_*air 5 .net c# long-filenames pathtoolongexception
如果这些文件的完整路径超过260个字符,则无法枚举目录中包含的文件。以下代码显示了该问题:
void TestLongPath(DirectoryInfo testDirectory)
{
if (testDirectory.Exists)
{
try
{
testDirectory.GetFiles("SomeFileNamePattern*");
}
catch (System.IO.DirectoryNotFoundException)
{
Console.WriteLine("Long path test failed for " + testDirectory.FullName);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的app.manifest文件包含:
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>
Run Code Online (Sandbox Code Playgroud)
但是所有要做的就是将错误从PathTooLongException更改为DirectoryNotFoundException。
这是我的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>
Run Code Online (Sandbox Code Playgroud)
我在Windows 10 Pro上,使用Visual Studio 2019 16.1.1。我的目标是.NET 4.7.2。
如何枚举这些超长目录中的文件?它们位于我无法控制的共享网络驱动器上,因此重命名目录对我来说不是一种选择。
我已经使用这里的信息解决了这个问题:目录的最大字符数或长度是多少?
对于 .NET 4.6.2 或更高版本:App.Config 中的“Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false”无效。
您不需要在路径前添加 \\?\ 前缀
您确实需要将注册表项 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled 设置为 1。
你确实需要
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>
Run Code Online (Sandbox Code Playgroud)
在你的 app.manifest 文件中。
即使启用了长路径,此代码似乎总是返回 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
101 次 |
| 最近记录: |