Skip to content

Instantly share code, notes, and snippets.

@nosajhpled
Last active August 31, 2018 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nosajhpled/b7e2cc531245fd2fa97a772b643e4500 to your computer and use it in GitHub Desktop.
Save nosajhpled/b7e2cc531245fd2fa97a772b643e4500 to your computer and use it in GitHub Desktop.
C# – Extract filename from a full path
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”
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment