C# app, Long Path support on Windows 10 post-1607, .NET 4.6.2.

Severian 6 Reputation points
2021-01-21T23:07:31.533+00:00

Hello,
I've got an app where long path support is now needed. It's running on Windows 10 post-1607, long paths have been enabled via group policy. When the app was targeting 4.6.1, it threw the PathTooLongException what a System.IO.FileInfo object is instatiated for a file with a very long path. The app is now targeting .NET 4.6.2, which is supposed to support long file names. I also enabled long paths on the Windows 10 machine in group policy and double checked that the value is being properly set in the registry.

With the app targeting 4.6.2, the app no longer throws the exception when a FileInfo object is instantiated. However, things still do not work properly with paths over 260 characters. To be clear, when I say it does not work, I mean that when the object is instantiated by the long name, dates stay at default, and the Length value indicates: "'FIfileInDir.Length' threw an exception of type 'System.IO.FileNotFoundException'".

I have already tried to set, within <runtime> in the app.config:
<AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false" />
That did not help.

Is the setting not properly applying on my system? Is there something I else I need to do to make long path support work?

Thanks in advance.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,040 questions
0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. Timon Yang-MSFT 9,591 Reputation points
    2021-01-22T07:26:09.39+00:00

    Try to add \\?\ before the path:

                string filePath = @"\\?\D:\test\......\ddddd.txt";  
                Console.WriteLine("Path length: "+ filePath.Length);  
                string str = File.ReadAllText(filePath);  
                Console.WriteLine(str);  
    

    59479-capture.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Aurélien N 21 Reputation points Microsoft Employee
    2021-11-04T10:34:14.933+00:00

    You have to modify the registry (or via GPO) and you have also to specify longPathAware in your application manifest file (and target .NET Framework 4.6.2 or above of course).
    To achieve it, add an "application manifest file" to your project, and uncomment the following:

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

    The manifest will be embedded in your application, and you won't need to add the \?\ prefix anymore to support long file paths.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.