Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use FileInfo class, avoiding PathTooLongException?

Tags:

c#

exception

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?

like image 451
Zanoni Avatar asked Aug 31 '09 13:08

Zanoni


2 Answers

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.

Update: Here even two ready solutions for this problem: AlpfaFS and Zeta Long Paths

like image 159
arbiter Avatar answered Sep 19 '22 07:09

arbiter


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.

like image 25
BFree Avatar answered Sep 18 '22 07:09

BFree