Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Zeta Long Paths

0.00/5 (No votes)
1 Oct 2018 4  
A .NET library to access files and directories with more than 260 characters length

Quick access

Introduction 

This article describes a library that provides several classes and functions to perform basic functions on file paths and folder paths that are longer than the "MAX_PATH" limit of 260 characters. 

Background

All .NET functions I came across that access the file system are limited to file paths and folder paths with less than 260 characters. This includes most (all?) of the classes in the System.IO namespace like e.g. the System.IO.FileInfo class.

Since I was in the need to actually access paths with more than 260 characters, I searched for a solution. Fortunately a solution exists; basically you have to P/Invoke Win32 functions that allow a special syntax to prefix a file and allow it then to be much longer than the 260 characters (about 32,000 characters).

The Library

So I started writing a very thin wrapper for the functions I required to work on long file names.

These resources helped me finding more: 

I started by using several functions from the BCL Team blog postings and added the functions they did not cover but which I needed in my project. 

Currently there are the following classes: 

  • ZlpFileInfo - A class similar to System.IO.FileInfo that wraps functions to work on file paths
  • ZlpDirectoryInfo - A class similar to System.IO.DirectoryInfo that wraps functions to work on folder paths 
  • ZlpIOHelper - A set of static functions to provide similar features as the ZlpFileInfo and ZlpDirectoryInfo class but in a static context 
  • ZlpPathHelper - A set of static functions similar to System.IO.Path that work on paths

Using the Code 

The project contains some unit tests to show basic functions. 

If you are familiar with the System.IO namespace, you should be able to use the classes of the library.

For example, to get all files in a given folder path, use the following snippet:

C#
var folderPath = new ZlpDirectoryInfo( @"C:\My\Long\Folder\Path" );

foreach ( var filePath in folderPath.GetFiles() )
{
    Console.Write( "File {0} has a size of {1}",
        filePath.FullName,
        filePath.Length );
}

When using the functions, please do not encode the paths with the long path syntax (like e.g. \\?\C:\my\path) but pass them as normal paths like c:\my\path or \\myserver\myshare\my\path. The library itself decides whether a path has to be converted and does it automatically. 

History 

  • 2016-09-27 - I've just discovered that .NET 4.6.2 now supports long paths natively. So if you are using my library you probably don't need it anymore if you target .NET 4.6.2 or above. (Or maybe it is not yet ready for prime time)
  • 2016-08-12 - First introduction of a .NET Core library (.NET Standard 1.6). See this NuGet package.
  • 2016-07-28 - Added functions to deal with short (8.3 "DOS") and long paths.
  • 2014-07-18 - Added functions like MoveFileToRecycleBin() to delete files and folders by moving them to the recycle bin.
  • 2014-06-25 - Also available on GitHub.
  • 2012-08-10 - Added several new methods.
  • 2011-10-10 - Fixed an error in ZlpFileInfo.Exists.
  • 2011-03-30 - Fixed a resource leak.
  • 2011-03-27 - Fixed an issue with WIN32_FIND_DATA.
  • 2011-02-22 - Added more functions.  
  • 2011-01-31 - Added functions MoveFile and MoveDirectory
  • 2010-03-24 - Added functions to get file owner, creation time, last access time, last write time.
  • 2010-02-16 - Maintenance release. 
  • 2009-11-25 - First release to CodeProject.com.  

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here