Handle Windows Long Paths Using C#

Windows file system imposes a default restriction on the maximum path length of 260 characters for folder/filename. It never allows a user to create a path beyond that limitation. However, you can create a long path by renaming the existing folder/filename.
In your system, if you have a long windows path and you want to retrieve the information of windows folders/files using windows APIs, an exception is thrown when a long path is reached.
To get windows folders or files info, commonly used options in .NET are
  • System.IO library. More details here
  • WMI. More information about WMI can be found here.
While trying to retrieve the data using above libraries for a long path.
Handle Windows Long Paths Using C#
With an unusual issue faced with longer paths, like,
a. Using WMI, The WMI query like “SELECT * FROM Win32_Directory Where Drive= ‘C:’”
It just returns the folders until it reaches a folder with long path. The rest of the remaining folders are skipped even though they have
small paths. You will never knowthat you have encountered an error in WMI process.
b. The System.IO library throws the below exception.
Handle Windows Long Paths Using C#


Enable NTFS long paths using group policy
Enabling the long paths will allow you to get the long path’s files or folders info using System.IO or WMI. If you’re using a Windows 10
machine, then please follow the below-mentioned steps to enable long paths
a. Hit the Windows key, type gpeedit.msc and press Enter.
b. Navigate to Local Computer Policy > Computer Configuration > Administrative Templates > System > Filesystem > NTFS.
c. Double click the Enable NTFS long paths option and enable it.
Handle Windows Long Paths Using C#
To reflect the changes, update the group policy. After successful group update, WMI or System.IO libraries work for long paths too.
But if you don’t have enough permissions to update the group policy then use the below solution
Use AlphaFS library
AlphaFS supports both long paths as well as the functionalities provided by System.IO library.
I'm sharing the output we were able to achieve using AlphaFS. Feel free to try out the other option as long as you have appropriate permissions to
update the group policy (enabling NTFS long paths using group policy)
Attaching the screenshot for your reference using AlphaFS.
Handle Windows Long Paths Using C#