Use Path.GetFileName
:
string full = @"C:xxxxxxxxxxxxabc.pdf";
string file = Path.GetFileName(full);
Console.WriteLine(file); // abc.pdf
Note that this assumes the last part of the name is a file - it doesn't check. So if you gave it "C:WindowsSystem32" it would claim a filename of System32, even though that's actually a directory. (Passing in "C:WindowsSystem32" would return an empty string, however.) You can use File.Exists
to check that a file exists and is a file rather than a directory if that would help.
This method also doesn't check that all the other elements in the directory hierarchy exist - so you could pass in "C:fooaraz.txt" and it would return baz.txt even if foo and bar don't exist.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…