C# – Extract filename from a full path

By | September 29, 2016

Over the years extracting the filename from a full path has always been a pain for me. Working with C#, I discovered a one line, easy to use, solution.


FileName = FileName.Split('\\')[FileName.Split('\\').Count() - 1];

Filename = “c:\TextFile.txt”

Will return:
“TextFile.txt”

And


FileName = (FileName.Split('\\')[FileName.Split('\\').Count() - 1]).Split(',')[0];

Filename = “c:\TextFile.txt”

Will return:

“TextFile”