I’m running the code below and getting exception below. Am I forced to put this function in try catch or is there other way to get all directories recursively?
I could write my own recursive function to get files and directory. But I wonder if there is a better way.
// get all files in folder and sub-folders
var d = Directory.GetFiles(@"C:\", "*", SearchOption.AllDirectories);
// get all sub-directories
var dirs = Directory.GetDirectories(@"C:\", "*", SearchOption.AllDirectories);
“Access to the path ‘C:\Documents and Settings\’ is denied.”
If you want to continue with the next folder after a fail, then yea; you’ll have to do it yourself. I would recommend a
Stack<T>(depth first) orQueue<T>(bredth first) rather than recursion, and an iterator block (yield return); then you avoid both stack-overflow and memory usage issues.Example: