1

I'm trying to get all the sub-directories of a Directory.

This is the line of code that throws an exception

var projectNames = Directory.GetDirectories("Z:").ToList();

The Exception is DirectoryNotFoundException I searched in Microsoft's Documentation and found that this Exception should only be thrown if the Directory is unmapped or does not exist, but here in this case the path exists.

I also tried to search for other reasons that can throw this exception and I found nothing.

3 Answers3

3

Try this, to get list of directories from drive

System.IO.DriveInfo di = new System.IO.DriveInfo(@"Z:\");
System.IO.DirectoryInfo dirInfo = di.RootDirectory;

var dir = dirInfo.GetDirectories();
Amit Verma
  • 2,450
  • 2
  • 8
  • 21
0

Try this for your new rquirement

System.IO.DriveInfo di = new System.IO.DriveInfo(@"Z:\");
System.IO.DirectoryInfo dirInfo = di.RootDirectory;

//Excluding Hidden and System Directories

List<string> dir = dirInfo.GetDirectories().Where(x=>x.Attributes== FileAttributes.Directory && x.Attributes!=FileAttributes.Hidden && x.Attributes!= FileAttributes.System).Select(x=>x.Name).ToList();
Amit Verma
  • 2,450
  • 2
  • 8
  • 21
0

I Found the Solution, by referring to this answer

I should Write the Full Path for the mapped drive such as this

var projectNames = Directory.GetDirectories(@"\\10.1.7.5\Projects").ToList();