Question file name too long

marcvs

Member
Joined
Mar 24, 2007
Messages
9
Programming Experience
1-3
Hello everyone

I am reading filenames and some details recursively, starting from a user selected folder.

VB.NET:
Dim Path As New DirectoryInfo(NewPath)
Dim File As FileInfo

' Get details for each file
For Each File In Path.GetFiles
...
Next

I tried scanning an entire drive to test the code and ran into a filename too long error. Admittedly the offending file was an IE temp file in the recycle bin followed by a bunch of post requests. But the file is there, so this should be a legal filename according to Vista:

VB.NET:
D:\$RECYCLE.BIN\S-1-5-21-2585337480-3818995355-2129148593-1000\$RB9T6L8\some_dir\some_user\Internet\Temporary Internet Files\Content.IE5\00HOU27X\rm.seasonal[2].recipe;abr=!webtv;dcopt=ist;rid=240114;kw=Indian;kw=Side;kw=Curry;kw=Breakfast;kw=ba;ad=N;kw=recipes;kw=food;kw=views;kw=240114;sz=728x90;tile=1;ord=7313262760627928

Any ideas how to read this filename? I could skip recycle bins, but i'm thinking this might crop up in other folders too.
 
There's a limit on the length of file names and folder paths that .NET apps can deal with. They may be legal as far as the OS is concerned but your app can't process them. I've also seen apps that, as far as I know, are not .NET apps that also have trouble with long paths. I don't know what the reason is for the limit but it's there. As the documentation says, the DirectoryInfo and FileInfo constructors throw a PathTooLongException if:
The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
 
For such issues, you can use Long Path Tool as well, it works good!

If you're going to resurrect a thread that is over 4 years old then you could at least provide a link to some information or a download for this Long Path Tool.
 
No need for any tool, you can instruct Windows to use Unicode versions of its IO APIs by prepending "\\?\" to the path:

In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.

After researching a bit more, it seems that GetFiles is actually the culprit here. There is no direct way to fix the problem. You could however write your own GetFiles method that recursively calls the Unicode version of the proper APIs.

This should normally not occur naturally, as Windows prevents you from creating paths that are too long by default.
 
Last edited:
Back
Top