What's up with directory.enumeratedirectory??
What's up with directory.enumeratedirectory??
What's up with directory.enumeratedirectory??
What's up with directory.enumeratedirectory??
What's up with directory.enumeratedirectory??
What's up with directory.enumeratedirectory?? What's up with directory.enumeratedirectory?? What's up with directory.enumeratedirectory?? What's up with directory.enumeratedirectory?? What's up with directory.enumeratedirectory?? What's up with directory.enumeratedirectory?? What's up with directory.enumeratedirectory?? What's up with directory.enumeratedirectory??
What's up with directory.enumeratedirectory?? What's up with directory.enumeratedirectory??
What's up with directory.enumeratedirectory??
Go Back  Xtreme Visual Basic Talk > > > What's up with directory.enumeratedirectory??


Reply
 
Thread Tools Display Modes
  #1  
Old 05-26-2011, 04:15 PM
gubbs gubbs is offline
Centurion
 
Join Date: Sep 2002
Location: Georgia
Posts: 112
Angry What's up with directory.enumeratedirectory??


Is anyone else as frustrated and disgusted with Microsoft's horrible efforts in trying to provide developers a function that will retrieve complete directory info and/or lists that won't throw an exception and kill the remaining enumeration just because the function hits a directory whose access is denied? I am absolutely flabergasted after upgrading two levels from VS2005 to VS2010 just to find out I'm essentially hosed and have to go back to my old VB6 (!!!) DIR directory tree recursion code to get a list of all subdirectories of a given root directory. I can't believe I wasted an entire day investigating this nonsense.
__________________
Gubbs
Reply With Quote
  #2  
Old 05-26-2011, 04:45 PM
AtmaWeapon's Avatar
AtmaWeaponWhat's up with directory.enumeratedirectory?? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

What are you going on about? Have you even tried anything? For Each is a simple syntax sugar over the enumerator pattern. If you want to do something like recover from an exception, you have to use the enumerator and handle exceptions:
Code:
        Dim fileEnumerable = Directory.EnumerateFiles("C:\Temp")
        Dim fileEnumerator = fileEnumerable.GetEnumerator()
        While True
            Try
                Dim filesLeft = fileEnumerator.MoveNext()
                If filesLeft Then
                    Dim file = fileEnumerator.Current
                    Dim fileContents = System.IO.File.ReadAllText(file)
                    Console.WriteLine("{0} characters.", fileContents.Length)
                Else
                    Exit While
                End If
            Catch ex As UnauthorizedAccessException
                Console.WriteLine("File error!")
            End Try
        End While
Yeah it's wonky. The method itself isn't actually the cause of the exception, though I think EnumerateDirectories() might get into trouble if the parent doesn't give you access. However, in that case what were you expecting? You're not allowed to see the contents, so it's not like the first item throws, and the next item, and the next item. No, that item throws because you can't see it and that's that.

I can't believe you wasted an entire day investigating it either. You should have asked for help earlier! How about you explain what you're trying to do and we can work on something that does it?
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 05-28-2011, 10:09 AM
gubbs gubbs is offline
Centurion
 
Join Date: Sep 2002
Location: Georgia
Posts: 112
Default

Guru,

This is what I'm trying to do: I need to inventory all subdirectories of a given root directory and return a list of directories to which the user has access.

Though the enumeratefiles example was appreciated, it doesn't apply to my situation. And as you suggest (and as I pointed out in my original statement), enumeratedirectories (alldirectories in my case) gets into trouble if an enumerator for-next loop encounters a directory to which access is denied - the exception is thrown at the 'For' line and the user is thrown out of the for-next loop, leaving an incomplete result set. I took your approach and modified it for directories rather than files and it did indeed throw an acception when a restricted-access directory was encountered and exited from the While Loop before all directories were inventoried.

I don't quite understand your point with your comments, "...However, in that case what were you expecting? You're not allowed to see the contents, so it's not like the first item throws, and the next item, and the next item. No, that item throws because you can't see it and that's that...." ??? No, wasn't expecting to "see" the directory. I was expecting that the enumerator would skip the directory and continue evaluating the rest of the directories to which the user DOES have access. I certainly wasn't expecting (and don't find the resulting action particularily usefull) that the enumeration would simply stop, leaving the rest of the directories to which the user DOES have access un-evaluated. Do you think that is usefull?

In real world corporate environments (especially FDA-regulated environments), there are reasons why users may have restricted access permissions to certain directories, leaving me with the old VB6 recursive DIR code examples as my only remedy to simply traverse all directories and to return a list of all directories to which the user has access (which, as expected, is very slow for large directories). Until I dug a little further.

Luckily, I was able to find an example (http://www.codeguru.com/columns/vb/a...in-VB-2010.htm) using a stack object and an iterative approach somewhat similar to the old vb6 recursive DIR examples available in the literature. As the author states, "Suppose you want to traverse all folders. Suppose further that if you encounter an exception you want to skip that folder and continue. Until there is an option like SearchOption.ContinueOnErrors or an attachable event that performs a semantically similar operation[,] there is no opportunity to continue on an exception when it only takes one method call to traverse sub-folders." The author's stack object approach works well (after a slight modification of the code to ensure that the very last toplevel directory in the defined root directory is evaluated).

I'm open to further suggested approaches - more than one way to skin a cat is more usefull than only one way to skin a cat.

I appreciate your help.
__________________
Gubbs
Reply With Quote
  #4  
Old 05-28-2011, 03:25 PM
AtmaWeapon's Avatar
AtmaWeaponWhat's up with directory.enumeratedirectory?? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Quote:
Originally Posted by gubbs View Post
Though the enumeratefiles example was appreciated, it doesn't apply to my situation. And as you suggest (and as I pointed out in my original statement), enumeratedirectories (alldirectories in my case) gets into trouble if an enumerator for-next loop encounters a directory to which access is denied - the exception is thrown at the 'For' line and the user is thrown out of the for-next loop, leaving an incomplete result set. I took your approach and modified it for directories rather than files and it did indeed throw an acception when a restricted-access directory was encountered and exited from the While Loop before all directories were inventoried.
Sometimes you have to use your brain when programming. The "handle exceptions from MoveNext()" pattern doesn't just work for one case, it works any time an enumerator could throw an exception. The linked article is sort of a logical progression; it could have been written with enumerators but it's not worth typing it out unless you just want to see it.

Quote:
I don't quite understand your point with your comments, "...However, in that case what were you expecting? You're not allowed to see the contents, so it's not like the first item throws, and the next item, and the next item. No, that item throws because you can't see it and that's that...." ??? No, wasn't expecting to "see" the directory. I was expecting that the enumerator would skip the directory and continue evaluating the rest of the directories to which the user DOES have access. I certainly wasn't expecting (and don't find the resulting action particularily usefull) that the enumeration would simply stop, leaving the rest of the directories to which the user DOES have access un-evaluated. Do you think that is usefull?
If it skipped directories you cannot access, how would you be able to tell? The return value is IEnumerable(Of String). What could it do with the string to make it clear it couldn't access that directory? If it returns Nothing, you won't know which directory access failed. If it adds some text to the end, it'll confuse newbies who don't know to expect it. The standard .NET contract is "Do what you say or throw an exception if you can't."

EnumerateDirectories() assumes you either expect to be able to access all directories or that you've verified them all. It fails at the first inaccessible item. This is no different from how GetDirectories() works, except that one doesn't return partial results so you get nothing at all if you fail. If you want to detect inaccessible directories, you have to use recursion or stacks and manually descend into each directory; that way when you get the exception you know which access failed. A ContinueOnErrors option seems useful until you realize "I want to know which directories I couldn't access" is true 99% of the time. If ContinueOnErrors existed, "How do I do this?" would have 1,000,000 "use continueonerrirs here is the codes" tutorials. That'd put customers expecting to be told that some directories weren't scanned by their application in a spot, because the kind of person who wants a copy/paste answer doesn't often think farther than the clipboard.

Quote:
In real world corporate environments (especially FDA-regulated environments), there are reasons why users may have restricted access permissions to certain directories, leaving me with the old VB6 recursive DIR code examples as my only remedy to simply traverse all directories and to return a list of all directories to which the user has access (which, as expected, is very slow for large directories). Until I dug a little further.
I'm not stupid and this isn't my first rodeo. When I had to discover this information I was playing in .NET 1.1 and didn't have EnumerateDirectories() or even lambdas to help out. I had to use worker threads so GetDirectories() wouldn't hang the UI. I had to handle network shares and the various problems that introduces. I had to deal with files that exceeded the maximum path restriction due to a maze of deeply nested "mount folder as drive" hierarchies. There weren't examples. I had to use my brain.

It's nice to reuse other people's work, but at some point you have to realize you've spent more time looking for copy/paste solutions than it would take to solve the problem yourself. Add the stuff you learned from this to your bag of tricks, and next time you might not have to bother looking for solutions.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.

Last edited by AtmaWeapon; 05-28-2011 at 03:45 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
What's up with directory.enumeratedirectory??
What's up with directory.enumeratedirectory??
What's up with directory.enumeratedirectory?? What's up with directory.enumeratedirectory??
What's up with directory.enumeratedirectory??
What's up with directory.enumeratedirectory??
What's up with directory.enumeratedirectory?? What's up with directory.enumeratedirectory?? What's up with directory.enumeratedirectory?? What's up with directory.enumeratedirectory?? What's up with directory.enumeratedirectory?? What's up with directory.enumeratedirectory?? What's up with directory.enumeratedirectory??
What's up with directory.enumeratedirectory??
What's up with directory.enumeratedirectory??
 
What's up with directory.enumeratedirectory??
What's up with directory.enumeratedirectory??
 
-->