runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Path.GetFileName has different logic from .NET 4.8 Framework

Open azhukov87 opened this issue 7 years ago • 3 comments

Hello,

I faced different behavior of Path.GetFileName in .Net core 2.2 and .Net.Framework 4.8

class Program
{
    static void Main(string[] args)
    {
        //NET Core 2.2
        var testNetCore1 = Path.GetFileName("test: 1/01/2019 test:.jpg"); //"2019 test:.jpg"
        var testNetCore2 = Path.GetFileName("test: 1/01/2019.jpg"); //"2019.jpg"
        var testNetCore3 = Path.GetFileName("test:.jpg"); //"test:.jpg"
            
        //NET Framework 4.8
        var testNetFramework1 = Path.GetFileName("test: 1/01/2019 test:.jpg"); //".jpg" (different from Net core: 2019 test:.jpg)
        var testNetFramework2 = Path.GetFileName("test: 1/01/2019.jpg"); //"2019.jpg" (equal to Net core: 2019.jpg)
        var testNetFramework3 = Path.GetFileName("test:.jpg"); //".jpg" (different from Net core: 2019 test:.jpg)
    }
}

azhukov87 avatar Jun 26 '19 08:06 azhukov87

cc: @JeremyKuhne

stephentoub avatar Jun 26 '19 13:06 stephentoub

@JeremyKuhne can provide more details on why the behavior is different. I will mark this issue as Future because there are known differences between the two implementations, which were improved in .NET Core.

carlossanlop avatar Jun 26 '19 15:06 carlossanlop

Thanks for the report! This is expected. .NET Framework incorrectly assumed that the colon was always a volume separator. What you gave are all filenames. "test:.jpg" is a valid filename on NTFS/ReFS. It refers to an alternate data stream on "test". The default data stream name is "" (or "test:" more specifically).

Path.GetFileName() now uses Path.GetPathRoot() which correctly handles the semantics of UNCs, DOS device paths, and traditional paths on Windows. I've filed an issue to clarify this in the documentation.

https://github.com/dotnet/dotnet-api-docs/issues/2644

JeremyKuhne avatar Jun 26 '19 17:06 JeremyKuhne