长路径\\?\解决方法不适用于某些安装 [英] Long path \\?\ workaround not working on some installs

查看:100
本文介绍了长路径\\?\解决方法不适用于某些安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的应用程序需要处理文件名/路径名非常长的文件.这是一个.Net 4.6应用程序,因此我已经实现了4.6.2之前的解决方法,以允许\\?\语法概述为此处.

The app I'm working on needs to handle files with very long file/path names. It's a .Net 4.6 application so I've implemented the pre-4.6.2 workaround to allow the \\?\ syntax as outlined here and here.

这是我用来启用该功能的代码(我无法修改app.config,因此必须在代码中进行设置):

This is the code I'm using to enable the feature (I can't modify the app.config so this has to be set in code):

var type = Type.GetType("System.AppContext");
if (type != null)
{
    AppContext.SetSwitch("Switch.System.IO.UseLegacyPathHandling", false);
    AppContext.SetSwitch("Switch.System.IO.BlockLongPaths", false);

    var switchType = Type.GetType("System.AppContextSwitches");
    if (switchType != null)
    {
        // We also have to reach into System.AppContextSwitches and manually update the cached private versions of these properties (don't ask me why):

        var legacyField = switchType.GetField("_useLegacyPathHandling", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
        legacyField?.SetValue(null, (Int32)(-1)); // <- caching uses 0 to indicate no value, -1 for false, 1 for true.

        var blockingField = switchType.GetField("_blockLongPaths", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
        blockingField?.SetValue(null, (Int32)(-1)); // <- caching uses 0 to indicate no value, -1 for false, 1 for true.
    }
}

这可以(是!)在我们测试过的所有机器上(除了一个)(嘘!)都可以.该计算机与其他计算机一样,是Windows 10 Pro安装,并且在[Computer \ HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ FileSystem]命名空间中具有相同的注册表设置.

This works (yay!) on all the machines we've tested on, except one (boo!). The machine in question is a Windows 10 Pro installation, like the others, and has the same registry settings in the [Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem] namespace.

此特定计算机上的错误消息是:

The error message on this particular machine is:

不支持给定的路径格式

The given path format is not supported

我们可以在该计算机上看到的一个区别是,在Windows File Explorer中查看一个很长的文件时,位置"字段使用r->属性"菜单中的\\?\语法.

The one difference we can see on that machine is that when looking at a very long file in Windows File Explorer, the 'Location' field uses the \\?\ syntax in the r-click > Properties menu.

我猜测是有一些注册表项导致文件资源管理器中的差异和修复失败,但不是上面提到的FileSystem名称空间.

I'm guessing that there's some registry key that is causing both that difference in File Explorer, and the failure of my fix, but somewhere other than the FileSystem namespace mentioned above.

有人遇到过类似的问题,或者对其他可能相关的注册表区域有所了解吗?

Has anyone encountered a similar issue, or have an idea of other registry areas that might be relevant?

推荐答案

如果不想在每个 App中设置这些AppContext开关,则可以通过注册表在计算机范围内设置这些开关.分别配置文件:

You can set those AppContext switches on a machine-wide basis via the registry if you don't want to set them in each App.config file individually:

这些设置将影响所有在其 App.config 文件中未指定其他值的 .NET 应用.也就是说,注册表设置仅更改默认值,通过指定<AppContextSwitchOverrides value="..." />

These settings will affect all .NET apps that don't specify a different value in their App.config file. That is, the registry setting only changes the default value, which can still be overridden with app-specific values by specifying <AppContextSwitchOverrides value="..." />


EnableLongPath.reg :

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AppContext]
"Switch.System.IO.BlockLongPaths"="false"
"Switch.System.IO.UseLegacyPathHandling"="false"


C:\>regedit.exe EnableLongPath.reg

这篇关于长路径\\?\解决方法不适用于某些安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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