Click here to Skip to main content
Click here to Skip to main content

Zeta Long Paths

By , 9 Aug 2012
 

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:

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. 

Summary   

This article quickly introduced a library to deal with long file names when accessing files and folders. 

Please note that the library currently is limited in the number of provided functions.

I will add more functions in the future, just tell me which you require, below in the comments section of this article. 

History 

  • 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Uwe Keim
Chief Technology Officer Zeta Producer Desktop CMS
Germany Germany
Member
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.
 
In his free time, he does climbing, running and mountain biking. Recently he became a father of a cute boy.
 
Some cool, free software from us:
 
Free Test Management Software - Intuitive, competitive, Test Plans. Download now!  
Homepage erstellen - Intuitive, very easy to use. Download now!  
Send large Files online for free by Email
Some random fun stuff in German

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberMauricevh24 Jan '13 - 1:21 
Great work, used it in a SQL CLR solution!
GeneralMy vote of 5memberHope W Lam9 Jan '13 - 4:27 
This is AWESOME. Thank you so much. This saved me tons of time and dealt with the issue I was having perfectly.
BugPInvokeHelper.MAX_PATH bugmemberdrussilla5 Dec '12 - 3:51 
Hello,
 
Now, PInvokeHelper.MAX_PATH set to 250, so paths that smaller that 250 char passes to Win Api functions without \\?\ prefix. But according to MSDN [^] max length for directories without prefix is 248.
When I try to create folder, that has 248-250 chars, using ZlpIOHelper.CreateDirectory() I got an error.
 
So valid value for PInvokeHelper.MAX_PATH is 247, it's cover files length limitation (260 chars) and folder length limitation (248 chars)
GeneralMy vote of 5memberJF201510 Aug '12 - 5:12 
Excellent article. Thanks for sharing your work with the community!
Suggestionx64 SupportmemberRichard Y9 Apr '12 - 12:07 
Was looking through your code and noticed that you are calling ToInt32() on your IntPtr handles. Handel's on an x64 machine can be 64bit Int's so may wish to call ToInt64() instead. On a 32 bit machine the pointer will never go above the 32bit boundary; but this will add support for 64bit OS's
GeneralRe: x64 SupportsitebuilderUwe Keim9 Apr '12 - 18:23 
Thanks, Richard. Is there a portable way of doing this or do I have to detect the bitness and switch-case accordingly?
Wollen Sie ganz einfach Ihre eigene Homepage erstellen, ohne HTML-Kenntnisse, einfach, professionell und mit viel Freude? Probieren Sie unser Desktop Content Management System (CMS) Zeta Producer für Windows aus. Komplett mit eigenem Shop, Gästebuch, Weblog, Bildergalerien, Integration von YouTube-Videos. Wir haben eine aktive Anwender-Community, schnellen Support, sympathische Support-Mitarbeiter.

GeneralRe: x64 SupportmemberZac Greve1 Jul '12 - 8:21 
You can use the System.Runtime.InteropServices.Marshal class or the System.Runtime.InteropServices.GCHandle class to get pointers to structures/classes/etc.
 
Look them up on MSDN for more info.
Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions.
Dave Barry
Read more at BrainyQuote[^]

GeneralRe: x64 Supportmembermrdance6 Aug '12 - 22:51 
I would really appreciated if you added this. Currently I cannot delete this directory structure in x64 environment:
 
c:\backup\02_Dati\Dati_31_07_2012\Archivio\Vss\Nicola\Projest\Export_WildOrchid_PurchaseDeliveryAdvice\Export_WildOrchid_PurchaseDeliveryAdvice\bin\Debug\Export_WildOrchid_PurchaseDeliveryAdvice.publish\Export_WildOrchid_PurchaseDeliveryAdvice_1_0
--- neteject.com - Internet Solutions ---

GeneralRe: x64 SupportsitebuilderUwe Keim9 Aug '12 - 18:59 
I've looked through my code and found the only occurances of Int32 when checking for error return values.
 
Still, I've uploaded a new version, in case the previous version was doing it differently.
Wollen Sie ganz einfach Ihre eigene Homepage erstellen, ohne HTML-Kenntnisse, einfach, professionell und mit viel Freude? Probieren Sie unser Desktop Content Management System (CMS) Zeta Producer für Windows aus. Komplett mit eigenem Shop, Gästebuch, Weblog, Bildergalerien, Integration von YouTube-Videos. Wir haben eine aktive Anwender-Community, schnellen Support, sympathische Support-Mitarbeiter.

QuestionZlpDirectoryInfomemberMember 860610429 Jan '12 - 21:26 
Hi,
Great lib.
 
I am using 1.0.0.3 but have a problem with ZlpDirectoryInfo.
Field Name is equal to FullName which is a bit unfortunate, for example when using Path.Combine.
 
Could you fix that so that ZlpDirectoryInfo.Name provides only the name of the dir, not the full path ?
 
Thanks
Paweł

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 10 Aug 2012
Article Copyright 2009 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid