代码之家  ›  专栏  ›  技术社区  ›  technophebe

长途跋涉?\解决方法不在某些安装上工作

  •  5
  • technophebe  · 技术社区  · 6 年前

    我正在开发的应用程序需要处理文件/路径名非常长的文件。它是一个.Net 4.6应用程序,所以我已经实现了4.6.2之前的解决方案,允许使用“\?”?\语法概述 here here .

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

    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\u LOCAL\machine\SYSTEM\CurrentControlSet\Control\FileSystem]命名空间中具有相同的注册表设置。

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

    不支持给定的路径格式

    我们在这台机器上看到的一个区别是,当在Windows文件资源管理器中查看一个很长的文件时,“Location”字段使用“\?”?\r-click>属性菜单中的语法。

    我猜是某个注册表项导致了文件资源管理器中的差异和我的修复失败,但不是上面提到的文件系统命名空间。

    有没有人遇到过类似的问题,或者对其他可能相关的注册领域有了想法?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Glenn Slayden    6 年前

    你可以把那些 AppContext 如果不想在每个 App.config软件 单独归档:

    enter image description here

    这些设置将影响所有 .NET版本 未在其 App.config软件 文件。也就是说,注册表设置只更改 默认值 ,仍然可以通过指定 <AppContextSwitchOverrides value="..." />


    启用longpath.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