Get File Extension from File Upload Control using C#

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.   
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />  
  6.   
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div class="container">  
  12.         <br />  
  13.         <br />  
  14.   
  15.   
  16.     <div class="row">  
  17.         <div class="form-inline">  
  18.             <label > File Upload</label>  
  19.             <asp:FileUpload runat="server" ID="file_upload" CssClass="btn-default" />  
  20.   
  21.         </div>  
  22.         <br />  
  23.   
  24.         <div class="form-inline">  
  25.             <asp:Button ID="btnGet" runat="server" Text ="Get Extension" onclick="btnGet_Click" CssClass="btn-primary" />  
  26.             <asp:Label ID="lblResult" runat="server" style="color:green"/>  
  27.         </div>  
  28.     </div>  
  29.     </div>  
  30.     </form>  
  31. </body>  
  32. </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
Ebook Download
View all
Learn
View all