How to get FileName and File Extension from URL using Asp.Net C# and VB.Net

← PrevNext →

I have previously shared a post explaining how to get the file name from a URL in JavaScript. While the previous example dealt with validations at the client side, now let’s see how we get the file name and file extension from a URL in Asp.Net using C# and VB.Net.

This is for .net programmers. You can use both client side and server side codes to get the file name and extension.

Get File Name and Extension in C#

var url = new Uri("https://www.encodedna.com/aspdotnet/get-file-size-using-csharp-and-vb.htm");
result.InnerHtml = "File name: " + System.IO.Path.GetFileName(url.LocalPath) + "<br /> " +
    "File type: " + System.IO.Path.GetExtension(url.LocalPath);

result.InnerHTML, its a <p> element, where I am showing the output.

In the examples above, I am using the Path.GetFileName() and Path.GetExtension() methods to get the file name and file type (or extension) of a given URL.

The methods are part of the Path class of System.IO name space.

Get File Name and Extension Vb.Net

Dim url = New Uri("https://www.encodedna.com/aspdotnet/get-file-size-using-csharp-and-vb.htm")

MsgBox(System.IO.Path.GetFileName(url.LocalPath))
MsgBox(System.IO.Path.GetExtension(url.LocalPath))
Here's an Example

Now lets check with more URLs, with different file types.

Let us assume I have a dropdown list with different URL's (web pages or images). When I select a URL from the list, it should get the file name and extension.

The Markup
<asp:DropDownList runat="server" id="ddl"
    AutoPostBack="True"
    OnSelectedIndexChanged="checkURL" 
    Font-Names="Calibri" Font-Size="Medium">

    <asp:ListItem>-- Select --</asp:ListItem>

    <asp:ListItem Value="https://www.encodedna.com/images/theme/easy-image-resizer.jpg">
        https://www.encodedna.com/images/theme/easy-image-resizer.jpg
    </asp:ListItem>

    <asp:ListItem Value="https://www.encodedna.com/aspdotnet/get-file-size-using-csharp-and-vb.htm">
        https://www.encodedna.com/aspdotnet/get-file-size-using-csharp-and-vb.htm
    </asp:ListItem>
                
    <asp:ListItem Value="https://www.encodedna.com/category/javascript/default.aspx">
        https://www.encodedna.com/category/javascript/default.aspx
    </asp:ListItem>
</asp:DropDownList>

<p id='result' runat="server"></p>
C# Code (to get the file name and extension from a URL)
protected void checkURL(object sender, EventArgs args)
{
    if (!string.IsNullOrEmpty(ddl.SelectedValue))
    {
        var url = new Uri(ddl.SelectedValue);
        result.InnerHtml = "File name: " + System.IO.Path.GetFileName(url.LocalPath) + "<br /> " +
            "File type: " + System.IO.Path.GetExtension(url.LocalPath);
    }
}
Vb.Net Code
Protected Sub checkURL(ByVal sender As Object, ByVal args As EventArgs)
    If ddl.SelectedValue <> "" Then
        Dim url = New Uri(ddl.SelectedValue)

        MsgBox(System.IO.Path.GetFileName(url.LocalPath))
        MsgBox(System.IO.Path.GetExtension(url.LocalPath))
    End If
End Sub

If you are using VB, you can show the result using the MsgBox() method. This method is not available in C#, therefore, I have used a <p> element to show the result.

The Path class provides many more methods and properties, to perform operations on files and directories. Such as, System.IO.Path.GetDirectoryName() etc.

Thanks for reading.

← PreviousNext →