question

Severian-0206 avatar image
1 Vote"
Severian-0206 asked AurelienN-MSFT published

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

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.

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
1 Vote"
TimonYang-MSFT answered AurelienN-MSFT commented

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.


capture.png (1.2 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

My understanding was the prefix should not be needed after the update in Windows 10 1607, and after long paths are then enabled in group policy. Can you confirm this?

0 Votes 0 ·

@Severian-0206
I am not sure about other versions, but for my current version of Windows 10 2004, I still need to add the prefix to use the long path. Modifying the registry or group policy is useless.
You can post a question in the Windows Forum-Microsoft Community to confirm the support of other versions for long path.

0 Votes 0 ·
AurelienN-MSFT avatar image
0 Votes"
AurelienN-MSFT answered AurelienN-MSFT published

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.