Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / Markdown

Zeta Long Paths

Rate me:
Please Sign up or sign in to vote.
4.86/5 (41 votes)
1 Oct 2018CPOL3 min read 137.3K   99   59
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.  
This article was originally posted at https://github.com/UweKeim/ZetaLongPaths

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Zeta Software GmbH
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions

 
GeneralMy vote of 5 Pin
JV999923-Feb-11 0:10
professionalJV999923-Feb-11 0:10 
GeneralMy vote of 5 Pin
SledgeHammer0122-Feb-11 11:10
SledgeHammer0122-Feb-11 11:10 
QuestionNew feature required Pin
Horscht200522-Feb-11 9:53
Horscht200522-Feb-11 9:53 
AnswerRe: New feature required Pin
Uwe Keim22-Feb-11 10:08
sitebuilderUwe Keim22-Feb-11 10:08 
GeneralRe: New feature required Pin
Horscht200522-Feb-11 10:12
Horscht200522-Feb-11 10:12 
GeneralRe: New feature required Pin
fjdiewornncalwe22-Feb-11 10:33
professionalfjdiewornncalwe22-Feb-11 10:33 
GeneralRe: New feature required Pin
Uwe Keim22-Feb-11 10:40
sitebuilderUwe Keim22-Feb-11 10:40 
GeneralMy vote of 4 Pin
R. Hoffmann8-Feb-11 8:36
professionalR. Hoffmann8-Feb-11 8:36 
I'd have liked to see a description of the calls/techniques used to implement the library, instead of just a brief introduction to the library itself. However, the usefulness of the library, along with the author's helpfulness and responsiveness to feature requests bring the rating up to 4, instead of say 2 Smile | :)

Thanks for contributing!
GeneralRe: My vote of 4 Pin
Uwe Keim22-Feb-11 10:08
sitebuilderUwe Keim22-Feb-11 10:08 
GeneralZeta Long Paths...new functionality requested Pin
michaeldjackson20-Mar-10 13:58
michaeldjackson20-Mar-10 13:58 
GeneralRe: Zeta Long Paths...new functionality requested Pin
Uwe Keim21-Mar-10 20:10
sitebuilderUwe Keim21-Mar-10 20:10 
GeneralRe: Zeta Long Paths...new functionality requested Pin
Uwe Keim23-Mar-10 19:15
sitebuilderUwe Keim23-Mar-10 19:15 
GeneralRe: Zeta Long Paths...new functionality requested Pin
michaeldjackson24-Mar-10 2:19
michaeldjackson24-Mar-10 2:19 
GeneralAdditions Pin
michaeldjackson22-Feb-10 13:47
michaeldjackson22-Feb-10 13:47 
GeneralRe: Additions Pin
Uwe Keim22-Feb-10 18:33
sitebuilderUwe Keim22-Feb-10 18:33 
Generalhttp://alphafs.codeplex.com/ Pin
Uwe Keim5-Jan-10 1:57
sitebuilderUwe Keim5-Jan-10 1:57 
GeneralLong awaited Pin
reisenklaus26-Nov-09 10:57
reisenklaus26-Nov-09 10:57 
GeneralRe: Long awaited Pin
Uwe Keim26-Nov-09 11:07
sitebuilderUwe Keim26-Nov-09 11:07 
GeneralAdditional features Pin
hardsoft25-Nov-09 8:32
hardsoft25-Nov-09 8:32 
GeneralRe: Additional features Pin
Uwe Keim25-Nov-09 8:50
sitebuilderUwe Keim25-Nov-09 8:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.