Open mklement0 opened 5 months ago
Tagging subscribers to this area: @dotnet/area-system-io See info in area-owners.md if you want to be subscribed.
This is also how Path.GetExtension works. What was your scenario that led you to find this? I wasn't expecting Extension
to be in FileSystemInfo but rather in FileInfo.
The problem is that the code stops as soon as it sees a directory separator. One possible way to solve this could be to trim the trailing separator.
I discovered it in the context of the linked PowerShell issue (https://github.com/PowerShell/PowerShell/issues/21553)
.NET, for better or worse, generally makes no distinction between directories and files with respect to what it considers the extension part of the name.
That Path.GetExtension()
behaves the way it does is defensible, but if you construct a System.IO.DirectoryInfo
instance with a trailing directory separator that otherwise behaves the same as without it (.Name
, .Exists
), I think it equally makes sense to ignore this incidental separator with respect to .Extension
.
Description
System.IO.DirectoryInfo
misreports theExtension
property for instances constructed with a trailing path (directory) separator: it unexpectedly returns the empty string.Note:
The same applies to
System.IO.FileInfo
, because the property is implemented inSystem.IO.FileSystemInfo
; however, even though a trailing path (directory) separator is also tolerated there, it is less likely to be used in practice.The trailing separator is (sensibly) otherwise ignored, e.g. with respect to
.Exists
and.Name
The behavior is the same for both
/
and\
, irrespective of platform.Reproduction Steps
Expected behavior
Both statements should return
"bar"
Actual behavior
The statement using the path with the trailing path (directory) separator unexpectedly returns
""
(the empty string).Regression?
No. Also affects .NET Framework.
Known Workarounds
No response
Configuration
.NET 8.0.4, .NET 9.0.0-preview.1.24080.9 All platforms (irrespective of whether
\
or/
i used).Other information
No response