4

Is there a simple method for getting a directory listing (e.g., DirectoryPathCollection) for a given directory path?

I'd like to be able to get all directories easily for the current directory as well as from a given path.

I'm not seeing anything obvious in Directory Operations reference, nor in DirectoryPath or DirectoryAliases.

Workarounds

So far, I've found a couple approaches that do work.

Using the GlobbingAliases.GetDirectories method with an argument to match all, but that seems like a bit of a hack. (It also has some quirks for trying to get sub-directories, but that is going to be a separate issue I'll investigate.)

var allDirectories = GetDirectories(@".\*");

Additionally, I have had luck just going directly to System.IO.Directory methods.

var allDirectories = System.IO.Directory.GetDirectories(".", "*", System.IO.SearchOption.TopDirectoryOnly).Select (d => new DirectoryPath(d));
patridge
  • 26,385
  • 18
  • 89
  • 135

1 Answers1

2

You can also use Context.FileSystem it has a GetDirectory it returns a IDirectory which has a GetDirectories method.

The advantage of using Cake's abstractions is that i.e modules can supply alternative implementations i.e long path support on system that don't support that with regular .Net

patridge
  • 26,385
  • 18
  • 89
  • 135
devlead
  • 4,935
  • 15
  • 36
  • Short of reading the source, is there a reference/guide for that `GetDirectories` method's `filter` parameter? Even a usage example there might be helpful. – patridge Jun 23 '17 at 16:53
  • Additionally, when should one be using `IDirectory` vs. `DirectoryPath`? Should all the places I use `DirectoryPath` be rewritten to target `IDirectory`? – patridge Jun 23 '17 at 16:55
  • IDirectory is "low level" , you'll mostly be using the globber GetDirectories and DirectoryPath which is what most aliases use for parameters too. – devlead Jun 23 '17 at 17:16