I need to get a list of files and directories corresponding to a given arbitrary path, e.g. \foo\bar\*.txt
. Essentially, the same result as would dir
with this path produce.
Ideally, it should be an array of FileSystemInfo
rather than of expanded file names/path strings.
When starting from a certain root directory, the solution is simply
a = new DirectoryInfo(root_path);
return a.GetFileSystemInfos(my_pattern);
But this time I don't have a defined root_path
; my_pattern
is arbitrary and can be relative, absolute or UNC path.
For simplicity, let's assume I don't need to parse wildcards recursively, i.e. paths\*\like*\this?.txt
. Wildcards can only appear in the last part (but the pattern can still match a file or a directory).
I could split my_pattern
into 'directory' and 'file' parts (using IO.Path.GetDirectoryName()
etc., and then treat them like above. But this feels overcomplicated for the task, given that I have to deal separately with cases when one of such parts is empty.
If I could get DirectoryInfo
for the 'computer root', from which even absolute paths would be valid, it would be easy, but this doesn't seem to be possible.
It feels like this should be a one-liner, but I can't find it in .NET...