x.Replace('\\','/')) .W" /> x.Replace('\\','/')) .W" /> x.Replace('\\','/')) .W"/>

Why do I get this IOException, when I'm not even trying to open the file?

110 Views Asked by At

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()));
0

There are 0 best solutions below