skipping access denied folders when using GetDirectories()
-
Hi all, i can use something like the code below to get all the directory within a given folder. DirectoryInfo di = new DirectoryInfo(@"C:\"); foreach (DirectoryInfo d in di.GetDirectories("*.*", SearchOption.AllDirectories)) { Console.WriteLine(d.Name); } but as soon as i hit a folder i dont have access to it crashes. is there anyway i can perform the same action but get it to skip folder i don't have access too? Many Thanks, Phil
-
Hi all, i can use something like the code below to get all the directory within a given folder. DirectoryInfo di = new DirectoryInfo(@"C:\"); foreach (DirectoryInfo d in di.GetDirectories("*.*", SearchOption.AllDirectories)) { Console.WriteLine(d.Name); } but as soon as i hit a folder i dont have access to it crashes. is there anyway i can perform the same action but get it to skip folder i don't have access too? Many Thanks, Phil
use a try/catch?
Help me! I'm turning into a grapefruit! Buzzwords!
-
Hi all, i can use something like the code below to get all the directory within a given folder. DirectoryInfo di = new DirectoryInfo(@"C:\"); foreach (DirectoryInfo d in di.GetDirectories("*.*", SearchOption.AllDirectories)) { Console.WriteLine(d.Name); } but as soon as i hit a folder i dont have access to it crashes. is there anyway i can perform the same action but get it to skip folder i don't have access too? Many Thanks, Phil
use try-catch inside the loop. That is what I do. BTW: .NET 4.0 will offer better methods to enumerate files/folders, without requiring a (possibly huge) array, so one will be able to really use the recursive mode. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi all, i can use something like the code below to get all the directory within a given folder. DirectoryInfo di = new DirectoryInfo(@"C:\"); foreach (DirectoryInfo d in di.GetDirectories("*.*", SearchOption.AllDirectories)) { Console.WriteLine(d.Name); } but as soon as i hit a folder i dont have access to it crashes. is there anyway i can perform the same action but get it to skip folder i don't have access too? Many Thanks, Phil
-
use try-catch inside the loop. That is what I do. BTW: .NET 4.0 will offer better methods to enumerate files/folders, without requiring a (possibly huge) array, so one will be able to really use the recursive mode. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
That will not work. The exception will occur while executing the
GetDirectories
method itself and control will never go inside thetry/catch
.:confused: It works for me, I am listing all files/folders of any device I choose (both XP and Vista). If GetDirectories() and GetFiles() would not cope with inaccessible files and folders, they would be completely useless, wouldn't they? :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
:confused: It works for me, I am listing all files/folders of any device I choose (both XP and Vista). If GetDirectories() and GetFiles() would not cope with inaccessible files and folders, they would be completely useless, wouldn't they? :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
In XP SP2, I created a folder(c:\TestFolder) and "denied" all the permissions under security tab. GetDirectories method itself will throw exception. Control never came to try/catch. See my code:
DirectoryInfo dirInfo = new DirectoryInfo(@"C:\\"); foreach (DirectoryInfo dir in dirInfo.GetDirectories("\*.\*", SearchOption.AllDirectories)) { try { } catch { } }
-
In XP SP2, I created a folder(c:\TestFolder) and "denied" all the permissions under security tab. GetDirectories method itself will throw exception. Control never came to try/catch. See my code:
DirectoryInfo dirInfo = new DirectoryInfo(@"C:\\"); foreach (DirectoryInfo dir in dirInfo.GetDirectories("\*.\*", SearchOption.AllDirectories)) { try { } catch { } }
That is new to me. I'll have to investigate. And what is your solution? :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
That is new to me. I'll have to investigate. And what is your solution? :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.