I have the following string:
string path = "C:\Users\Username\Desktop\FileName.pdf";
I need to take off FileName.pdf
and put in a string variable called fileName.
The code has to be generic in a sense that anytime i get path, i start going from the en开发者_如何学运维d of the string down to the first backslash.
I'm using C#
You can use System.IO.Path
to manipulate a path.
GetFileName Returns the file name and extension of the specified path string.
For completeness here's the code:
string fileName = Path.GetFileName(path);
Use a System.IO.FileInfo object - you can get the filename using this easily.
Well you can use Split('\') and you get the last of the array
精彩评论