Click here to Skip to main content
14,984,060 members
Articles / Desktop Programming / Windows Forms
Posted 15 Sep 2011

Tagged as


28 bookmarked

Handle Long Paths in .NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
15 Sep 2011CPOL2 min read
A class written to easily handle long paths (32K in length) using .NET.


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.


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.


  • 2011-09-14 - First submission.


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
No Biography provided

Comments and Discussions

QuestionCopy function Pin
Member 1365708228-Nov-20 3:28
MemberMember 1365708228-Nov-20 3:28 
QuestionToo much thanks Pin
Member 1333027218-Feb-19 3:12
MemberMember 1333027218-Feb-19 3:12 
AnswerHandle Long Paths Pin
gregoria1-Oct-15 2:25
Membergregoria1-Oct-15 2:25 
GeneralMessage Closed Pin
11-Oct-16 18:03
MemberMember 1278856911-Oct-16 18:03 
GeneralMessage Closed Pin
11-Oct-16 18:05
MemberMember 1278856911-Oct-16 18:05 
AnswerMessage Closed Pin
11-Oct-16 18:08
MemberMember 1278856911-Oct-16 18:08 
Questionhow to delete long path file Pin
gregoria1-Oct-15 2:25
Membergregoria1-Oct-15 2:25 
QuestionOne small addition Pin
Member 1152355613-Mar-15 12:31
MemberMember 1152355613-Mar-15 12:31 
QuestionTHANK YOU Pin
Member 1067376630-Nov-14 23:13
MemberMember 1067376630-Nov-14 23:13 
Questionlong path Pin
andrewop pit25-Sep-14 7:59
Memberandrewop pit25-Sep-14 7:59 
SuggestionFindFirstFileEx Pin
kornman0030-Jun-14 3:03
Memberkornman0030-Jun-14 3:03 
SuggestionVery nice Pin
Navy037712-Sep-13 3:08
MemberNavy037712-Sep-13 3:08 
Questionhow to delete long path file Pin
rockybart18-Jan-13 19:46
Memberrockybart18-Jan-13 19:46 
QuestionRe: how to delete long path file Pin
Nathan Stiles24-Jan-13 16:35
MemberNathan Stiles24-Jan-13 16:35 
QuestionAnother one Pin
Richard Deeming22-Sep-11 6:38
mveRichard Deeming22-Sep-11 6:38 
AnswerRe: Another one Pin
drweb8627-Sep-11 23:30
Memberdrweb8627-Sep-11 23:30 
GeneralRe: Another one Pin
Richard Deeming19-Oct-11 9:13
mveRichard Deeming19-Oct-11 9:13 

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.