PathTooLongException work around
Someone just came to me with an issue they were having with a classic ASP application that is using the FileSystemObject to display files as links.
Everything was working great, until all of the sudden some files were not showing up. Come to find out, the root cause was due to the fact that the FileSystemObject, just like Windows Explorer, is limited to a MAX_PATH of 260 characters for any full path and file name. Since there was already error handling (and hiding) in the Classic ASP code, this was not obvious at first. What really made it very obvious was when Windows Explorer wouldn't allow a file to be renamed past the 260 character limit.
Windows Vista Ultimate RTM - Windows Explorer has the same limitation.
The files that were exceeding this limit had been copied to this location via an automated Robocopy.
I whipped up a proof of concept .NET ASPX WebForm using DirectoryInfo - GetFileSystemInfos(). But alas, with this method it was generating the following exception:
Exception Details: System.IO.PathTooLongException: The path is too long after being fully qualified. Make sure path is less than 260 characters.
After further research, the only solution I discovered was to use the Windows API via PInvoke of CreatFileEx. This API utilizes unicode for the path and can handle up to 32,000 characters. Supposedly appending the string "\\?\" in front of the path should force that path to be sent as unicode, but I was unable to get this to work successfully. Instead, this was causing an invalid character exception. I will continue looking at this alternative considering the preformance improvements that would come along with it. It's just a matter of getting the unicode part working, here's a resource I discovered that showed some VB.NET code - http://vbnet.mvps.org/index.html?code/fileapi/fsoapicompare.htm.
After more trial and error on my own, I did finally discover the easiest work around and that is to simply use these two methods of the .NET framework instead:
System.IO.Directory.GetFiles()
System.IO.Directory.GetDirectories()
They both return back string arrays of the full path of the files and directories respectfully. As long as you don't need the additional properties, as in this case, this is the easiest solution to the problem.
Comment Notification
If you would like to receive an email when updates are made to this post, please register here
Subscribe to this post's comments using
Comments
Leave a Comment
About dwalker
David Walker has over 15 years experience in application development with over 50% of that employed as a consultant with companies such as: Microsoft, IBM Global Services, Texaco, Winner Communications (ESPN.com), and Bank of Oklahoma. At the age of 14, he began his application development ambitions with a Commodore 64, BASIC, and a 300 baud modem. Even at that early age, he primarily focused on two specific application types: multi-user communities and database applications.
His hunger to learn as much as possible about development lead him through courses such as DBase III, DBase IV, Pascal, C, C++, Java, and several in UNIX. He started his development career first doing heavy processing with Access and VBA, then moved on to VB 3, Oracle, and Delphi. Visual Basic was one environment that remained constant for many years, including his very first .NET projects performed in Visual Basic.NET.
After working several years on very high end internal Corporate applications, the consultant company he was working for, sought out his ideas for actual software products that could be packaged and sold. He had already developed several prototypes of a dynamic portal application, before portals even became popular, so this became the logic decision and he became the Director of Product Development. Under his direction, a team of developers and graphic artists, took a skinning approach before that become popular, and completed the core portal application, and continued on to developer 15+ add-on modules, including things such as: Help Desk Ticket Systems, Change Control, Records Management, Human Resources, and many more applications. Eventually, it spun off into it's own separate company as KnowledgeGEAR, a complete intranet in the box solution.
Having worked as a consultant, he has had a experience with a very wide range of applications and architectures, at one time, even converting several Fox Pro and GW-Basic applications to VB 6 and ASP. His early training of Unix and the C language and years of experience with JavaScript, lead him very quickly to C#, where he has remained focused ever since.
He is the past President of the
Tulsa Developers .NET user group, founder and past President of the
Tulsa SQL Server Group and
Tulsa Java Developers Group, past Vice-President and Director of Membership Services of the
Tulsa SharePoint Interest Group.
He has been the director/chairman of the
TulsaTechFest event since it's inception in 2006. He coordinated the first Tulsa Code Camp and then rebranded it two years in a row as
School of Dev!
He is the founder and director of
NEOTECHCouncil (NorthEast Oklahoma Technology Council).
He was a board member, Vice-President of the Speakers Bureau for
INETA NORAM from June 2007 to June 2009.
He has been an MCP since 2003 and MCAD and MCSD since 2005. He is currently pursuing his MCDBA and then on to MCSE.
He was a Microsoft MVP - ASP/ASP.NET Developer from June 2007 - June 2009.
On June 22, 2009, he joined Microsoft as a ADC (Application Developer Consultant).