Long path \\?\ workaround not working on some installs

与世无争的帅哥 提交于 2020-01-24 04:03:23

问题


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.

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.
    }
}

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

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.

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?


回答1:


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:

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


来源:https://stackoverflow.com/questions/53193280/long-path-workaround-not-working-on-some-installs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!