none
Get Filetype without extension

    Question

  • How can I get the filetype of a file without looking a the extension(eg. for when somebody has uploaded a png with the jpg extension).

    I know this is possible on *NIX systems with the 'file' command.

    But I have to do it on Windows with C#.

     

    Wednesday, October 13, 2010 12:26 PM

Answers

All replies

  • hi,

     

    you need to get MIME info from file. please read this:

    http://stackoverflow.com/questions/58510/using-net-how-can-you-find-the-mime-type-of-a-file-based-on-the-file-signature

     

    A list of MIME type can be found here: http://www.feedforall.com/mime-types.htm


    BR, Karol. mark as answer/vote as helpful if it helped you
    Wednesday, October 13, 2010 12:49 PM
  • Hi CoffeeJunk,

    Welcome to MSDN Forums!

     

    If your question has been solved, please don’t forget mark/vote the replies which helped you, and this will encourage the other community members to join in discussion and help each one.

     

    If there’s anything unclear, please feel free to let me know.

     

    Have a nice day!

    Mike

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    Friday, October 15, 2010 8:30 AM
  • Use Path class from System.IO

    string fileName = @"C:\mydir.old\myfile.ext";
    string path = @"C:\mydir.old\";
    string extension;
    
    extension = Path.GetExtension(fileName);
    Console.WriteLine("GetExtension('{0}') returns '{1}'", 
      fileName, extension);
    
    extension = Path.GetExtension(path);
    Console.WriteLine("GetExtension('{0}') returns '{1}'", 
      path, extension);
    
    
    Friday, October 15, 2010 10:56 AM
  • Read the file and check the first few hundreds of bytes to match
    known binary pattern, specifications etc.
    E.g. all ZIP-Files start with PK, exe/dll with MZ,
    rar with !Rar, jpeg with JFIF,
    MPEG with hex: 00 00 01 BA 21
    AVI with sth like RIFF/Á+AVI LIST
    Lots of work if you start from scratch!


    Chris
    Sunday, October 17, 2010 11:01 PM
  • try 
    {
    // Open a Stream and decode a JPEG image
    
    using (Stream imageStreamSource = new FileStream("tulipfarm.jpg"
    , FileMode.Open, FileAccess.Read, FileShare.Read))
    {
      JpegBitmapDecoder decoder = new JpegBitmapDecoder(imageStreamSource, 
    BitmapCreateOptions.PreservePixelFormat,
     BitmapCacheOption.Default);} }
    catch (FileFormatException)
    {//If it is not a JPG, then it will throw FileFormatException<br/>
    
    //so here you can flag it as "not a JPG" otherwise, it is a JPG<br/>
    
    }
    
    The above will work for the JPG/PNG scenario.  In general, you could just try opening the file with the usual codec or program and if an exception is thrown, then you could guess that it's not the correct format, but you may not know exactly what it is.
    Monday, October 18, 2010 2:10 AM
  • The following doesn't need to catch an exception:

    using (var image = Image.FromFile(@"tulipfarm.jpg"))
    {
      if (image.RawFormat.Equals(ImageFormat.Jpeg))
      {
        // image is jpeg
      }
      else
      {
        // image is not jpeg
      }
    }
    
    Monday, October 18, 2010 7:55 AM
  • But what do you do if the file is of type docx, or pptx? There is no current MIME Type for it? The FindMimeFromData won't return you the correct MIME type (if extension is not specified). Is there another way to figure out the type of the file without relying on the extension?
    Thursday, October 11, 2012 4:48 PM
  • In principle you can open folder using FS, read in loop each file disregarding extensions. In header of each file you can get type and throw into your procedure or identification functionality.

    There is application(open source) which does this for indexing purposes called solr, more details about this app in  lucidworks.com.

    Thursday, October 11, 2012 7:58 PM