Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
I have done some extensive research about this problem and it seems that there is no work around because of the netframe work and OS set limit on address lengths. So my question is how can I exclude folders that exceed this limitation and simply move to the next file or folder when trying to scan directories?
 
thank you in advance Smile | :)
Posted 10-Feb-11 15:36pm
Dale 20121.8K
Comments
SAKryukov at 10-Feb-11 21:48pm
   
Again, no exception dump, no code. What kind of help are you hoping for?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You cannot have directory/file names in your system with the path name exceeding MaxPath. The exception could be a result of some bug during path calculation.
 
[EDIT]
File system names are valid it they are obtained using Directory.GetFiles or Directory.GetDirectories. These methods guarantee that the obtained names represent file system objects which "physically" present on the file system. Such name should always obey the name length limitation rules, otherwise they could not be created.
 
If the names are composed using some different method (which can easily happen if you need to compose a name to create new file/directory programmatically, the name could appear too long and cause this exception.
[END EDIT]
 
Please see my comment to the Question. Where is the information? This kind of bug is usually quite easy to find and fix.
 
—SA
  Permalink  
v2
Comments
Dale Seeley at 10-Feb-11 22:26pm
   
I have had no problem with the scanning code but I did have alot of desktop files folders and what not that i have catagorized into one folder which is how I know this error is authentic and deals with the max path exception but my question is how to skip folders and files with names exceeding this value?
SAKryukov at 10-Feb-11 22:43pm
   
As I say: something's must be wrong in your code. Why do you think you're posing right question? You think you need to skip something, but I say there should not be such thing. Post relevant part of your code and exception done! If you don't do that, there is nothing to discuss -- just yet.
--SA
Dale Seeley at 11-Feb-11 1:05am
   
Ok the Exception is:
 
The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
 
and the line on error is:
 
Dim fi As FileInfo = New FileInfo(file1)
Dale Seeley at 11-Feb-11 1:07am
   
can we now discuss the problem or is there something I need to add?
SAKryukov at 11-Feb-11 12:37pm
   
1) Are you sure exception was caused by this exact line? (You did not do exception dump as I asked. In exception dump you could print exception stack (highly recommend) and the stack would show all relevant line numbers in the code.)
 
2) Are you sure the string value of the variable "file1" represent existing file? Please see my update in this Answer, between [EDIT] and [END EDIT]
 
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Typically, the single most common reason why recursive searches of hard disks fail is when certain directories require permissions that the calling code does not have.
 
Update
~~~~~~~~
 
Okay, unfortunately the classes in System.IO do not use Unicode paths (where you prefix "\\?\" to the path). You can avoid using System.IO and instead P/Invoke the API directly and use the W versions of the functions (and then use Unicode paths).
 
Example article that does this:
.NET 2.0 Workaround for PathTooLongException[^]
Although the article explains a different API, you will need to do something similar.
 
Alternatively, just skip files that exceed the max-path (but I reckon you will end up skipping too many files if you go that route). The hardcoded value for MaxPath in System.IO is 260 (Windows ANSI filepath limit).
  Permalink  
v2
Comments
Dale Seeley at 10-Feb-11 22:20pm
   
a good answer but I am most positive that the problem is not related to access denied. The exception clearly states a maxpath exception but thank you for the insight.
 
I will still rate your answer
Nishant Sivakumar at 10-Feb-11 22:25pm
   
Do you mind pasting the exact text of the error message?
Dale Seeley at 10-Feb-11 22:42pm
   
I will need one min to get this for u brb
Nishant Sivakumar at 10-Feb-11 22:43pm
   
Ok, thanks.
Dale Seeley at 10-Feb-11 23:01pm
   
Ok the Exception is:
 
The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
 
and the line on error is:
 
Dim fi As FileInfo = New FileInfo(file1)
Nishant Sivakumar at 10-Feb-11 23:10pm
   
Okay, I've updated my answer, Dale.
Dale Seeley at 11-Feb-11 0:44am
   
How might I go about skipping the files? Im not to concerned about how many folders are skipped because I do not expect many folders and files to go beyond the limitation.
 
thank you in advance and also thank you for the link and quick responce!
Dale Seeley at 11-Feb-11 1:00am
   
A few more questions about this link you have given if i might...
 
1) I am unsure how to use the source code file
2) CreateFileW is what?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Well, you can use Long Path Tool for such problems, it works perfectly to handle long file names problems.
  Permalink  
Comments
CHill60 at 14-Jul-14 11:17am
   
Answering questions that are over 3 years old with a reference to a paid-for utility is normally considered spam. I haven't reported it as such in this instance because your post is so vague and you did not include a link. I would advise not to do it again though.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 14 Jul 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100