-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Description
Description
File system entries obtained using a path argument in the shape of a "drive's current directory" e.g. C: were incorrectly formed by combining directory path + separator + entry name. We now avoid adding the separator with such paths to return the correct path for the entries.
Version
.NET 8 GA
Previous behavior
string pathToEnumerate = "C:";
Console.WriteLine($"Full path of \"{pathToEnumerate}\" is {Path.GetFullPath(pathToEnumerate)}.");
Path.GetFullPath(pathToEnumerate);
Console.WriteLine($"Enumerating files and folders in \"{pathToEnumerate}\".");
foreach (string entry in Directory.GetFileSystemEntries(pathToEnumerate))
{
Console.WriteLine(entry);
}
// Output
/*
Full path of "C:" is C:\Users\dacantu\consoleapps\Program
Enumerating files and folders in "C:".
C:\Program.csproj
C:\Program.sln
C:\bin
C:\obj
C:\Program.cs
*/Notice the enumerated entries are shown as if they were in the drive's root.
New behavior
Using above code snippet, the output for .NET 9 is the following.
Full path of "C:" is C:\Users\dacantu\consoleapps\Program.
Enumerating files and folders in "C:".
C:Program.csproj
C:Program.sln
C:bin
C:obj
C:Program.cs
Type of breaking change
- Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
- Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code may require source changes to compile successfully.
- Behavioral change: Existing binaries may behave differently at run time.
Reason for change
Users were reporting that this behavior was not correct, and it was also a regression from .NET Framework.
Recommended action
Windows users relying on enumeration of paths like C: should re-evaluate their application's IO operations. IMO, this is an unusual scenario unlikely to be used in production. Most users wanting to enumerate the current directory would use Environment.CurrentDirectory instead.
Feature area
Core .NET libraries
Affected APIs
System.IO.Directory.EnumerateFiles*
System.IO.Directory.EnumerateDirectories*
System.IO.Directory.EnumerateFileSystemEntries*
System.IO.Directory.GetFiles*
System.IO.Directory.GetDirectories*
System.IO.Directory.GetFileSystemEntries*
System.IO.DirectoryInfo.EnumerateFiles*
System.IO.DirectoryInfo.EnumerateDirectories*
System.IO.DirectoryInfo.EnumerateFileSystemEntries*
System.IO.DirectoryInfo.GetFiles*
System.IO.DirectoryInfo.GetDirectories*
System.IO.DirectoryInfo.GetFileSystemEntries*
System.IO.Enumeration.FileSystemEnumerable<TResult>.ctor(string, FileSystemEnumerable<TResult>.FindTransform transform, EnumerationOptions?)