I have the following code:
DirectoryInfo directoryInfo = new DirectoryInfo( path );
IEnumerable<FileInfo> files = directoryInfo.EnumerateFiles( "*.*", SearchOption.TopDirectoryOnly );
foreach( FileInfo file in files )
{
}
Sometimes, this gives a
DirectoryNotFoundException
. Since all I am doing in effect is asking .NET to give me a list of subdirectories it finds in a given directory, I don't understand how an immediate enumeration of these can decide one of them cannot be found. It seems illogical.
Can anyone explain what may be going on or at least where I should focus my investigation?
What I have tried:
Although the application is being run simultaneously by a handful of users on a LAN, directories are rarely deleted or renamed and so it is extremely unlikely this error is being caused by another user doing something at exactly the wrong moment.
The only explanation offered on Microsoft's help is that this exception could be caused by an unmapped network drive. All these folders are indeed on a mapped network drive but are all on the same drive. Therefore, if the parent path I use to construct the
DirectoryInfo
is present then any subfolders should also be accessible.