如何从C#中的文件路径获取子字符串?

saf*_*afi 3 .net c# string substring filepath

我有一个 string path = c:\inetpub\wwwrroot\images\pdf\admission.pdf

我正在使用这个

path = path.LastIndexOf("\\").ToString();
path = path.Substring(path.LastIndexOf("/") + 1);
Run Code Online (Sandbox Code Playgroud)

我想得到:

c:\inetpub\wwwrroot\images\pdf
c:\inetpub\wwwrroot\images\pdf\admission.pdf
Run Code Online (Sandbox Code Playgroud)

现在我想从这里得到录取.pdf string path我该怎么办?

Bal*_*a R 10

string path = "c:\\inetpub\\wwwrroot\\images\\pdf\\admission.pdf";

string folder = path.Substring(0,path.LastIndexOf(("\\")));
                // this should be "c:\inetpub\wwwrroot\images\pdf"

var fileName = path.Substring(path.LastIndexOf(("\\"))+1);
                // this should be admin.pdf
Run Code Online (Sandbox Code Playgroud)


Sea*_*ter 7

类上有一堆辅助方法,System.IO.Path用于从字符串中提取路径/文件名的部分.

在这种情况下,System.IO.Path.GetFileName会得到你想要的.