I tried this code:
var HighestFolder = Directory.GetDirectories("U:/New/Items", "*", SearchOption.AllDirectories)
.Select(x => x.Replace('\\','/'))
.Where(x => x.Split('/').Length >= 5)
.OrderByDescending(x => long.Parse(x.Split('/').Last()))
.FirstOrDefault();
I expected this to return the sub directory with the highest number. But instead, it throws this exception:
System.IO.IOException: The system cannot open the file: 'U:\New\Items\0\7917'
The folder is clearly valid and accessible and the app created it just moments earlier, and nothing I am seeing should be opening it at all. Why do I get an IOException?
Work around: I tried the following code which works successfully for my purposes, but doesnt explain why the previous one is failing.
var HighestFolder = Directory.GetDirectories(path)
.SelectMany(x => Directory.GetDirectories(x))
.Select(x => x.Replace('\\', '/'))
.Where(x =>x.Substring(path.Length).Split('/').Length>= 3)
.OrderByDescending(x => long.Parse(x.Split('/').Last()));