Get File Extension from File Upload control in the web form just by using Path.GetExtension Property in C#
Step 1: Create a web form in your project, In my case I named it as FileExtension.aspx and write the following design in it.
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head runat="server">
  4. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div class="container">
  10. <br />
  11. <br />
  12. <div class="row">
  13. <div class="form-inline">
  14. <label > File Upload</label>
  15. <asp:FileUpload runat="server" ID="file_upload" CssClass="btn-default" />
  16. </div>
  17. <br />
  18. <div class="form-inline">
  19. <asp:Button ID="btnGet" runat="server" Text ="Get Extension" onclick="btnGet_Click" CssClass="btn-primary" />
  20. <asp:Label ID="lblResult" runat="server" style="color:green"/>
  21. </div>
  22. </div>
  23. </div>
  24. </form>
  25. </body>
  26. </html>
Step 2: Write the following C# code in FileExtension.cs.
  1. protected void btnGet_Click(object sender, EventArgs e)
  2. {
  3. string strpath = System.IO.Path.GetExtension(file_upload.FileName);
  4. lblResult.Text = "Uploaded File Extension: " + strpath;
  5. }
Result in Browser:
Figure 1
Figure 2
Figure 3