Click here to Skip to main content
15,397,981 members
Articles / Programming Languages / C#
Article
Posted 10 Dec 2007

Tagged as

Stats

94.7K views
2.6K downloads
20 bookmarked

.NET 2.0 Workaround for PathTooLongException

Rate me:
Please Sign up or sign in to vote.
3.67/5 (11 votes)
17 Dec 2007CPOL
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.

C#
//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)

Share

About the Author

Balaji Lakshmanan
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
QuestionGreat code! and a question Pin
langalibalele19-Mar-12 0:23
Memberlangalibalele19-Mar-12 0:23 
GeneralMy vote of 5 Pin
hoernchenmeister1-Feb-12 21:02
Memberhoernchenmeister1-Feb-12 21:02 
AnswerAlternative: SUBST Pin
Juy Juka28-Nov-11 22:51
MemberJuy Juka28-Nov-11 22:51 
GeneralThanks for posting, but a few tips Pin
Geert van Horrik21-Jun-10 21:26
MemberGeert van Horrik21-Jun-10 21:26 
GeneralFileMode.Create Pin
Henk Meijerink19-Aug-09 5:38
MemberHenk Meijerink19-Aug-09 5:38 
GeneralCreateFileW For browsing directories with more than 256 Pin
diego jose25-Apr-08 12:23
Memberdiego jose25-Apr-08 12:23 
GeneralRe: CreateFileW For browsing directories with more than 256 Pin
Chintan.Desai21-Sep-10 23:34
MemberChintan.Desai21-Sep-10 23:34 
Generalcan't create file with really long name Pin
mezhaka4-Apr-08 1:38
Membermezhaka4-Apr-08 1:38 
Generalnice tip Pin
dmihailescu12-Dec-07 10:53
Memberdmihailescu12-Dec-07 10:53 

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.