Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi... i want to ask something,,

how to get file extension by filename?

i have code below:
C#
string id = "34";
string ext = System.IO.Path.GetExtension(Server.MapPath(Request.ApplicationPath + @"\Images\") + id);
Response.Write(ext);


revise my code please...
Posted
Updated 28-Sep-11 21:20pm
v2
Comments
Ade.Ruyani.Z 29-Sep-11 3:36am    
sorry every one,,i have a file 34.png n iwant get fileextension in folder images, and find that file extension just only call a filename? can i do that?
Muralikrishna8811 29-Sep-11 5:05am    
We can

I posted the code check it once

Hi,

I tried some code for you to get file extension by using filename

C#
DirectoryInfo dinfo = new DirectoryInfo(Server.MapPath("~/Images"));
           FileInfo[] finfo = dinfo.GetFiles("winter.*");
           int l=finfo.Length;
           string ext=Path.GetExtension(finfo[0].FullName);


Here you've to pass your file name instead of "winter"

Then The file information stored in finfo array.

So we can get that file extension

I hope you understood What I said

All the Best
 
Share this answer
 
Comments
Ade.Ruyani.Z 29-Sep-11 5:17am    
this great!! Muralikrishna, it's work and very helpful for me or the other... thanks you... ^_^,,
P.Salini 29-Sep-11 5:20am    
Yes this is working . Good one.
Your file name "...\Images\34" has no extension.
 
Share this answer
 
Comments
Ade.Ruyani.Z 29-Sep-11 3:29am    
yes.. i have a file 34.png,, can i just get fileextension with only call filename?
To get File Extension,
C#
Response.Write(Path.GetExtension(@"F:\Articles\ValidationControls\ValidationControls.zip"));


Refer below link

http://codes.codedigest.com/CodeDigest/68-Get-File-Extension--File-Name--File-Name-without-Extension-from-Path-in-C--and-ASP-Net.aspx[^]
 
Share this answer
 
v5
To get extension:

string ext = System.IO.Path.GetExtension(this.File1.PostedFile.FileName);

or

VB
Response.Write(Path.GetExtension(@"F:\Articles\ValidationControls\ValidationControls.zip"));
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900