0

How can I find the type of the file from the directory C#? I am reading the files from the directory and would like to find out if the file type is an image. Please let me know.

Thanks..

4 Answers 4

3

If fileInf is of type IO.FileInfo

System.IO.Path.GetExtension(fileInf.FullName) 

or

fileInf.Extension 
Sign up to request clarification or add additional context in comments.

Comments

1

One way to determine the file type (presuming you dont just want to use the GetExtension() method) is to use File Magic Numbers, here and here.

TrIDNet has a great XML database of magic numbers.

Comments

0

how about this

string extension= System.IO.Path.GetExtension("s.jpeg"); if ((extension == ".jpeg") || (extension == ".jpg") || (extension == ".bmp")) { //true } else { //false } 

Comments

0

If you get the extension, there should be a way to look up the file type registered to that extension in Windows. I'm not sure how to do the latter off hand.

Comments