Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to parse out image name from image url

Tags:

c#

url

If I have any URL with an image file at the end, such as:

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

And I want to get:

image1.jpg

What is the best way to do this in C#?

like image 237
leora Avatar asked Nov 01 '10 21:11

leora


3 Answers

string fileName = Path.GetFileName(@"http://www.google.com/test/test23/image1.jpg");
like image 50
Fredrik Hedblad Avatar answered Nov 06 '22 05:11

Fredrik Hedblad


As long as your sure its an image file you can just use the path class.

System.io.path.GetFilenAME

like image 40
rerun Avatar answered Nov 06 '22 05:11

rerun


public static void doSomething() {
    string url = "http:\\www.google.com\image1.jpg";
    string imageFileName = url.Substring( url.LastIndexOf( '\\' ) + 1 );
}
like image 1
Lorenzo Avatar answered Nov 06 '22 05:11

Lorenzo