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

Handle Long Paths in .NET

By , 15 Sep 2011
 

Introduction

This class allows for easy access to long path names in Windows. This is useful if you've ever run into exceptions because the path you're trying to access is too long. Your applications will no longer be limited by the 250~ character length limit.

Background

I set out to build a project in my free time. I won't get into the details but, I was stopped in my tracks, my plan ruined by the max length limit. Fortunately, I found the article series Long Paths in .NET, Part 1 of 3 [Kim Hamilton]. I used ideas from that article series to come to this simple solution to my problem.

Using the Code

I tried to emulate some of the functionality and method names of the File and Directory classes in the System.IO namespace as closely as possible. The namespace is System.IO.LongPath. The two classes are File and Directory.

The File class contains the following public methods:

  • void Delete(string file)
  • Deletes the specified file.

  • FileStream Open(string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share)
  • Opens a file stream to the file specified in the path argument.

  • void Copy(string sourceFile, string destinationFile, bool failIfDestinationExists)
  • Copies a file from one location to another.

  • void Move(string sourceFile, string destinationFile, bool failIfDestinationExists)
  • Moves a file from one location to another.

The Directory class contains the following public methods:

  • void Create(string directory)
  • Creates a directory with the given name.

  • void Delete(string drectory)
  • Deletes the specified directory and all sub-directories and files.

  • bool Exists(string drectory)
  • Checks if the specified directory exists.

  • string[] GetFileSystemEntries(string drectory)
  • Gets file system entries for the directory provided by the directory argument. This is not a recursive function, only file system entries for the provided directory are returned.

Points of Interest

This is the largest venture I've made into the Windows API. I don't know exactly what I'm doing. For example, I copied the attribute:

[return: MarshalAs(UnmanagedType.Bool)]

I copied this from Kim Hamilton's article and used it on the additional bool functions I imported from the Windows API. I don't know if I should be doing that or not. Feedback on that would be interesting.

You must specify all paths as long name paths by using \\?\c:\long\path and network paths as \\?\UNC\server\long\path.

You may not use relative paths. All paths specified must be absolute. ".." and "." are not supported.

I'm interested in any feedback. Please share your thoughts with me.

History

  • 2011-09-14 - First submission.

License

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

About the Author

Nathan Stiles
Web Developer
United States United States
Member
No Biography provided

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   
Questionhow to delete long path filememberrockybart18 Jan '13 - 19:46 
I have downloaded Long Path Tool and it helped me a lot .
http://LongPathTool.com[^]
QuestionRe: how to delete long path filememberNathan Stiles24 Jan '13 - 16:35 
Is this tool somehow usable from code? Does it have an API? Is source available?
QuestionAnother onememberRichard Deeming22 Sep '11 - 6:38 
One you might have missed: Uwe Keim's[^] Zeta Long Paths[^]



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


AnswerRe: Another onememberdrweb8627 Sep '11 - 23:30 
ZetaLongPath is bad component because of GNUx license. It cannot be used with applications without head acke.
GeneralRe: Another onememberRichard Deeming19 Oct '11 - 9:13 
I don't know if it's changed recently, but the article[^] says it's using the CPOL license[^], which is the same as this article.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 15 Sep 2011
Article Copyright 2011 by Nathan Stiles
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid