dotnet-api-docs
dotnet-api-docs copied to clipboard
Path.GetFileName behavior change on .NET Core
In .NET Core we consider Path.GetPathRoot() when finding the filename. The concept of Path.VolumeSeparatorChar is only relevant on Windows 9x machines. So:
.NET Framework
The characters after the last directory separator character in path. If the last character of path is a directory or volume separator character, this method returns Empty.
.NET Core
The characters after the last directory separator character after the path root (see Path.GetPathRoot) in path. If the path only contains a root, returns empty.
We should add to Path.VolumeSeparatorChar that we recommend not using it directly as it is a Win9x anachronism. Use Path.GetPathRoot to determine path roots.
This comment is also misleading:
Because \ is a legal file name on Unix, GetFileName running under Unix-based platforms cannot correctly return the file name from a Windows-based path like C:\mydir\myfile.ext, but GetFileName running under Windows-based platforms can correctly return the file name from a Unix-based path like /tmp/myfile.ext, so the behavior of the GetFileName method is not strictly the same on Unix-based and Windows-based platforms.
It will always return the correct filename based on the semantics of the current operating system. C:\ has no special meaning for Unix, but does for Windows. UNCs and DOS device paths are also Windows specific.