To get the filename of a url in C# and VB.NET you can use the following snippet.

Sample C#

public static string GetFilenameFromUrl(string url)
{
	return String.IsNullOrEmpty(url.Trim()) || !url.Contains(".") ? string.Empty : Path.GetFileName(new Uri(url).AbsolutePath);
}

Sample VB.NET

Public Shared Function GetFilenameFromUrl(url As String) As String
	Return If([String].IsNullOrEmpty(url.Trim()) OrElse Not url.Contains("."), String.Empty, Path.GetFileName(New Uri(url).AbsolutePath))
End Function

One thought on “How to get the filename of url in C# and VB.NET”

Leave a Reply