3

As you know files have extensions and mime types. but these two attributes are not intelligent enough two determine exact type of file. For example I have a .exe file and i rename it's extension two .png. So if I try to find out it's type or mime type programmatically the result will be images/png or .png but I want to read the header of file and find out exact file type. Is it possible programmatically with C# language?

Edit

Of course some questions are similar to mine but as you know files starts with a hex string, for example :

"JPEG", ".jpg", 0xFF, /*0xD*/ 0xD8, 0xFF, 0xE0, null, null, 0x4A, 0x46, 0x49, 0x46, 0x00

"Graphics Interchange Format 87a", ".gif", 0x47, 0x49, 0x46, 0x38, 0x37, 0x6

and etc.

Do file types have similar starting hex format?

Behzad Hassani
  • 2,129
  • 4
  • 30
  • 51
  • Yes, but you should know exactly what is the content of the so called 'header' in any kind of existing file. – Steve Dec 07 '15 at 08:15
  • You probably need to limit the scope of the question; if you want to test for png particularly is well documented (spec here: http://www.w3.org/TR/PNG/) so you could write a parser just for that. – zaitsman Dec 07 '15 at 08:16
  • Have you read this: http://stackoverflow.com/questions/58510/using-net-how-can-you-find-the-mime-type-of-a-file-based-on-the-file-signature, http://stackoverflow.com/questions/4177922/how-to-determine-file-type and http://stackoverflow.com/questions/11547654/determine-the-file-type-using-c-sharp? – Yeldar Kurmangaliyev Dec 07 '15 at 08:16
  • You can check the [file type signatures.](https://gyorgybalassy.wordpress.com/2013/08/04/mime-types-sniffing-in-c-sharp/) – Umut Seven Dec 07 '15 at 08:17
  • May be [Guessing a file type based on its content](http://codereview.stackexchange.com/questions/85054/guessing-a-file-type-based-on-its-content) would be interesting question to have a look on – Mohit S Dec 07 '15 at 08:18
  • There's already some existing file format analyzers, maybe you can try its command line by C# if it has command line function. – Lei Yang Dec 07 '15 at 08:22

1 Answers1

1

You can try checking for certain file signatures or magic numbers in the files. Here's the link for list of known file signatures and seems quite up to date:

There is another way of doing the same. Use Winista MIME Detector.

There is one XML file mime-type.xml that contains information about file types and the signatures used to identify the content type. You will need this file to create instance of MimeTypes object. Once you have created MimeTypes object, then call GetMimeType method to get MimeType of the stream. If the mime type could not be determined then a null object is returned from this method. Following code snippet demonstrates use of the library.

Example :

  MimeTypes g_MimeTypes = new MimeTypes("mime-types.xml");
    sbyte [] fileData = null;
    using (System.IO.FileStream srcFile = 
        new System.IO.FileStream(strFile, System.IO.FileMode.Open))
    {
        byte [] data = new byte[srcFile.Length];
        srcFile.Read(data, 0, (Int32)srcFile.Length);
        fileData = Winista.Mime.SupportUtil.ToSByteArray(data);
    }
    MimeType oMimeType = g_MimeTypes.GetMimeType(fileData);
Rahul Nikate
  • 6,192
  • 5
  • 42
  • 54