nav100
nav100

Reputation:

Find the file type

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..

Upvotes: 0

Views: 470

Answers (4)

CmdrTallen
CmdrTallen

Reputation: 2282

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.

Upvotes: 1

Jon Seigel
Jon Seigel

Reputation: 12401

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.

Upvotes: 0

anishMarokey
anishMarokey

Reputation: 11397

how about this

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

Upvotes: 0

Tim Santeford
Tim Santeford

Reputation: 28111

If fileInf is of type IO.FileInfo

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

or

fileInf.Extension

Upvotes: 3

Related Questions