5

I'm using C# with .NET v4.7.2 and Windows 10 and I have some files with long path (>260 characters in the paths) to copy.

I know there is a solution to prefix the path by \\?\

This prefix is working, but I do not want to prefix everytime for any file operation. since .Net v4.6.2 there is be a better solution by AppContext-Switches UseLegacyPathHandling and BlockLongPaths.

However this is not working.

My app.config looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
  </startup>
  <windowsSettings>
    <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
  </windowsSettings>
  <runtime>
    <AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false" />
  </runtime> 
</configuration>

My C# looks like this:

public static void Main(string[] args)
{
   string src = @"c:\Temp\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.txt";
   string dst = @"c:\Temp\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-123.txt";
   File.Copy(src, dst);
}

My Problem

  • With .NET v4.5, File.Copy() throws a System.IO.PathTooLongException

  • With .NET v4.7.2 File.Copy() throws a System.IO.DirectoryNotFoundException

I checked by AppContext.TryGetSwitch() if the switches are set, and they are. So I don't know how to get I to work.

What am I doing wrong?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
jaz
  • 228
  • 2
  • 14
  • 4
    You also have to convince the operating system that your program can handle long paths. That's done with the windowsSettings element, but it is in the wrong place. Not in the .config file, it belongs in the application manifest. If you don't already have one then use Project > Add New Item > Application Manifest File. https://blogs.msdn.microsoft.com/jeremykuhne/2016/07/30/net-4-6-2-and-long-paths-on-windows-10/ – Hans Passant May 23 '19 at 22:08
  • thank you for your input - I'll going to try that. – jaz May 27 '19 at 22:45

1 Answers1

2

Your filename is 317 characters long, which exceeds the 255 characters allowed in an NTFS path segment (#1, #2).

(In retrospect, it was probably a mistake for Jeremy to use an example that couldn't possibly work on any filesystem in #2!)

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
  • Thank you for your comment. So the really only way in a filesystem to have filenames longer than 255 is to prefix it with "\\?\" ??? – jaz Jan 10 '19 at 16:12
  • Refer to the comment from @hans-passant to get an idea of how this can be done on Win10. – Cahit Dec 22 '20 at 22:25