I am using the following code
var allFolderPaths = Directory.EnumerateDirectories(this.sourceFolder, "*.*", SearchOption.AllDirectories);
The issue I am having , is during a foreach loop (iterating over the allFolderPaths variable as shown above), it is failing because the directory is not found.
Now, the following code is, by itself not looking at directories etc, it’s just a string loop:
foreach (string folder in StringList)
{
/dostuff
}
However, when I update the code to use the allFolderPaths (As per the first example in this post)
foreach (string folder in allFolderPaths)
{
/dostuff
}
it fails due to “access to a path denied”!
So, does this mean the variable allFolderPaths is not actually ‘assigned’ at the point it is assigned to (if that makes any sense)?
Directory.EnumerateFiles() returns an IEnumerable<> which is ‘executed’ only when you actually enumerate the enumerable…(aka “lazy execution”).
If you want to force the EnumerateFiles to execute right-away, you can add a .ToList() (aka “memoization”).
Example:
The reason for the AccessDenied exception is that you’re trying to enumerate a directory that you don’t have permission to enumerate; Are you looking in ‘My Documents’ or similar?