如何从图片网址中解析出图片名称

时间:2010-11-01 21:34:18

标签: c# url

如果我在结尾处有任何带有图像文件的URL,例如:

http://www.google.com/image1.jpg
http://www.google.com/test/test23/image1.jpg

我想得到:

image1.jpg

在C#中执行此操作的最佳方法是什么?

4 个答案:

答案 0 :(得分:24)

string fileName = Path.GetFileName(@"http://www.google.com/test/test23/image1.jpg");

答案 1 :(得分:5)

只要您确定它的图像文件,您就可以使用路径类。

System.io.path.GetFilenAME

答案 2 :(得分:1)

public static void doSomething() {
    string url = "http:\\www.google.com\image1.jpg";
    string imageFileName = url.Substring( url.LastIndexOf( '\\' ) + 1 );
}

答案 3 :(得分:1)

不是最好的,但肯定是一种方式

url = "blahblah\image1.jpg"

string imageName  = url.substring(url.lastindexof("\") + 1);