-1

i try to get list of paths from directory symlink. It's write exception

Could not find a part of the path.

var filePath = @"C:\symlink";

var paths = new List<string>((Directory
  .GetFiles(filePath, "*.*", SearchOption.AllDirectories))
  .OrderBy(x => new FileInfo(x).Name));

1 Answers1

1

You have to check if directory exists; e.g. if we want to get an empty list when the directory is abscent:

var filePath = @"C:\symlink";

var paths = Directory.Exists(filePath)
  ? Directory
      .EnumerateFiles(filePath, "*.*", SearchOption.AllDirectories)
      .OrderBy(file => Path.GetFileName(file))
      .ToList()
  : new List<string>();  

Please, note that we don't have to use GetFiles(...) which reads all the files into an array but can just enumerate the files with EnumerateFiles

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • The directory contains batch of sub folders and files. The directory symlink is target from network driver. – Kosta Sidorenko Jun 18 '19 at 08:14
  • 1
    @Kosta Sidorenko: if you want to scan all subdirectories then `SearchOption.AllDirectories` is the right option; please, check, if `symlink` is a *correct map* of network drive, i.e. if system can address it as a *directory*. – Dmitry Bychenko Jun 18 '19 at 08:17