Windows 10 Maximum Path Length Limitation

2.9k Views Asked by At

I am, like some before me, confused by the Maximum Path Length Limitation. I successfully transferred a tree of files to a NAS using robocopy and a new Windows 10 machine.
On the source machine all files were in 260 characters limit. They were copied with robocopy to the path \nas-3tb-backup\Public\Save_2019 from the path e:. I wanted to check it afterwards with a small C# program but I don't succeed. Both FileInfo and File.OpenRead can't find the file that has an extended path by the NAS name. All programs like Notepad, Visual Studio Code, Windows Explorer have no problems displaying or opening this file. I have tried to use the prefix \\?\ which is shown here: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN#maxpath But the attempt with \\?\\\nas-3tb-backup---- failed. There is still a hint in the linked article to extend the path when the registry is changed. I want to avoid this because then I would have to change all Windows computers and since the Windows own programs work without registry hack it should be possible for me.

string fn1 = @"\\?\\\nas-3tb-backup\Public\VeryLongPathToFile";
bool b1 = new FileInfo(fn1).Exists;
if (b1) {
    using (FileStream stream = File.OpenRead(fn1)) {
    var b = stream.ReadByte();
    }
}
2

There are 2 best solutions below

1
On

Have you changed the app.config file in your C# solution as described here .NET 4.6.2 and long paths on Windows 10.

In this post he created a folder at a very long path but maybe it will help you as well.

The code he used:

<runtime>
  <AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false" />
</runtime>
0
On

Solution

Create a Manifest File

The app.manifest file must contain

<application xmlns="urn:schemas-microsoft-com:asm.v3">
  <windowsSettings>
    <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
  </windowsSettings>
</application>

No changes necessary in the config file or computer policy (maybe for Win32 apps but not tested)

Prefix \\?\ don't work for UNC Pathes Local Pathes like c:\ must be written as local UNC Path e.g. \\?\C:\

Test System

Windows 10 Ver. 1909 .Net Framework 4.7 C# .Net Console App 64 Bit