Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 604
technophebe Avatar asked Nov 07 '18 16:11

technophebe


People also ask

How do I fix installation path is too long?

1] Press Win + R to open the Run window and type the command regedit. Press Enter to open the Registry Editor. 3] In the right-pane, double-click on (or right-click >> Modify) LongPathsEnabled. 4] Set the Value data to 1 and click on OK to save the settings.

How do I bypass the path of a file too long?

(if the path is too long) First copy the folder to upper levels in windows explorer and then move it to your local computer. (if file names are too long) First try to zip/rar/7z them with an archive application and then copy the archive file to your local computer and then extract the contents. Use third party apps.


1 Answers

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:

enter image description here

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
like image 188
Glenn Slayden Avatar answered Oct 07 '22 19:10

Glenn Slayden