Get File Name from URL (C#/.NET)

To get the file name from a URL like

string URL = "";
string FileName = URL.Substring(URL.LastIndexOf("/") + 1,
    (URL.Length - URL.LastIndexOf("/") - 1));

FileName will now contain project.exe


Tim GauntJune 22nd, 2007 at 10:09 pm

Seeing as you’re not validating the end of the file name you can leave off the length param:

string FileName = URL.Substring(URL.LastIndexOf(”/”) + 1);



JoppeFebruary 5th, 2010 at 3:41 pm

Or: string fileName = Path.GetFileName(url);

VadimSeptember 28th, 2010 at 12:38 pm

Uri uri = new Uri(url);
string fileName = uri.Segments[uri.Segments.Length-1];

vijaiJanuary 26th, 2011 at 5:53 am

Path.GetFileName(url); seems right thanks,

NilukaAugust 2nd, 2012 at 6:04 pm

Thanks… This is relly help full

arunMarch 25th, 2013 at 5:17 pm

thank you

free jav hdApril 7th, 2015 at 9:03 am

Thanks… This is relly help full

Leave a comment

Your comment