views:

531

answers:

3

How can I use (to avoid PathTooLongException):

System.IO.FileInfo

with paths bigger than 260 chars?

Are there similar classes/methods that return the same result of FileInfo class?

+2  A: 

.NET 2.0 Workaround for PathTooLongException

rahul
+1  A: 

From what I know it is not easily possible. While it is possible to use workaround for streams as phoenix mentioned, it is not possible for file names handling. Internally every class that works with file names perform checks for long file names.

You can instantiate FileInfo and fill private memebers using reflection (however this is not recommended) and get FileInfo pointing to file with long path. But when you try to use this object you will still receive PathTooLongException exceptions, because for example, Path class (used heavily by FileInfo) checks for long path on every method call.

So, there is only one right way to get problem free long path support - implement your own set of classes that will mimic FileInfo behavior. It is not very complex (only security maybe), but time-consuming.

arbiter
+4  A: 

Here at work we deal with long paths quite frequently, and we therefore had to basically roll our own System.IO to do it. Well not really, but we rewrote File, Directory, FileInfo, DirectoryInfo and Path just to name a few. The basic premise is that it's all possible from a Win32 API perspective, so all you really need to do at the end of the day is invoke the Unicode versions of the Win32 API functions, and then you're good. It's alot of work, and can be a pain in the ass at times, but there's really no better way to do it.

BFree
how do your new classes look, are they 100% hand written or do you subclass the built in File class within your File class?
Allen
Well, File is actually a static class so you can't inherit from it, but most of the classes you'd think of subclassing (FileInfo, DirectoryInfo) are sealed, so you can't subclass them. We wrote them all from scratch. I won't lie, we did use Reflector ALOT :)
BFree
Doh, yeah, static :) Pretty cool, great answer.
Allen