Click here to Skip to main content
Licence CPOL
First Posted 10 Dec 2007
Views 27,025
Downloads 554
Bookmarked 17 times

.NET 2.0 Workaround for PathTooLongException

By | 17 Dec 2007 | Article
Using the Unicode version of CreateFileW, we can overcome the PathTooLongException error on file operations.

Introduction

File operations like File.OpenRead or the FileStream constructor would throw a PathTooLongException when presented with an absolute path of length greater than 260 characters.

Looking at the Win32 API, I found that the CreateFileW (Unicode) version of the function supports long file names up to 32000 characters if the file path is prefixed with a \\?\. I went ahead and created this class that has Open methods similar to File.Open/OpenRead/OpenWrite to make the interface consistent.

Background

In ANSI mode of the CreateFile API, Windows restricts the total file length to 260 characters. You can see this effect when you try to create a new file and rename to something more than 260 characters. Windows will automatically limit you to the total file path=260, starting from c:\.

Using the code

I have attached a class in the System.IO namespace called Win32File. It has the same interface as File.Open/OpenRead/OpenWrite.

//Sample Usage

FileStream fs1= Win32File.Open( "Long file name of greater " + 
                "than 260 char length", FileMode.Open);
StreamWriter sw = new StreamWriter(fs1);
sw.WriteLine("Hello world");
sw.Close();
sw.Dispose();

History

  • Version 1.0.
  • Version 1.1 -- Updated code to accommodate UNC paths. The prefix needs to be \\?\UNC\.

License

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

About the Author

Balaji Lakshmanan

Software Developer (Senior)

United States United States

Member



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. (secure sign-in)
 
Search this forum  
 FAQ
    Layout  Per page   
  Refresh
QuestionGreat code! and a question Pinmemberlangalibalele0:23 19 Mar '12  
GeneralMy vote of 5 Pinmemberhoernchenmeister21:02 1 Feb '12  
AnswerAlternative: SUBST PinmemberJuy Juka22:51 28 Nov '11  
GeneralThanks for posting, but a few tips PinmemberGeert van Horrik21:26 21 Jun '10  
GeneralFileMode.Create PinmemberHenk Meijerink5:38 19 Aug '09  
GeneralCreateFileW For browsing directories with more than 256 Pinmemberdiego jose12:23 25 Apr '08  
GeneralRe: CreateFileW For browsing directories with more than 256 PingroupChintan.Desai23:34 21 Sep '10  
Generalcan't create file with really long name Pinmembermezhaka1:38 4 Apr '08  
Generalnice tip Pinmemberdmihailescu10:53 12 Dec '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 17 Dec 2007
Article Copyright 2007 by Balaji Lakshmanan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid