Long paths in C#

By default, Windows uses a maximum length for paths that is 260 characters. This includes the drive letter – for example “C:\” – and the terminating null character. So this leaves you up to 256 characters for a path. This limitiation is defined due to the restrictions of the Windows API.

You can however use longer path names, that are up to 32767 characters. Within C#, there are serveral ways to achieve this:

  • By adding \\?\ in front of a path;
  • Using Windows 10 version 1067 or higher, updating the systems registry and your application manifest.

Adding “\\?\” in front of the path

You can simply add "\\?\" in front of the path you are using. In this way you’re telling the Windows API to use longer paths.

The big main advantage is that you don’t have to configure your computer (or another computer that needs to run your application) to support long paths.

This construction however can’t be used when using relative paths. Another drawback of this construction is that you have to update your code.

Windows 10 version 1067 or higher

Starting with Windows 10 version 1067, the Windows API does support longer path names. But before you can use these additional characters, you have to perform some steps:

  • Make sure the registry key “Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled” is set to 1
  • Your C# application should have an application manifest which includes the “longPathAware” element set to true:

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

The benefit from this strategy is that you don’t have to update your code when using longer paths. You only have to change the configuration of both your computer and your application.

Drawback is that the long paths will only be supported when the registry setting is set. When exporting your application to another computer, you have to be sure you update the registry over there to ensure your application runs successfully.

Also within the use of unit tests this will give issues. The code you want to test is stored within a component, this is invoked by a unit test framework. By using the application manifest strategy to enable long paths, this means that the calling party is the unit test framework. This framework should somehow enable the support of long paths, when you want to use them also within your unit tests. The best solution to deal with this is not to use directly disk access in your unit tests anyhow, but stub or mock it. This is also a better design principle.

In the end

In the end it will be still quite tricky to use long path names. A lot of other applications can have difficulties to access these long paths. So if it is possible, you should try to keep your paths up to the 256 characters when your application writes files to disk and you want to be sure also other applications can access them.

Posted in Uncategorized by Bruno at January 30th, 2023.

Leave a Reply