Bablu Dutt
Bablu Dutt

Reputation: 359

How to identify the original file extensions or mimetype of a file

There are possibilities of an .exe file being renamed to a .txt file to bypass any file type validations. I am looking for a way to find out the actual file type by reading the header of a file without using dlls like urlmon.dll.

MimeMapping.GetMimeMapping doesn't solve the problem, it just extracts the mime type based on the extensions.

Is there a dictionary which says what combinations of bytes represents atleast the very common file types such as txt, doc, docx, pdf, xls or xlsx , an exe etc?

Upvotes: 1

Views: 945

Answers (2)

mausworks
mausworks

Reputation: 1625

I think you sort of answered your own question.

This is a little bit of a pickle. Read the file-header signature, and see if it matches that of its extension. Using a FileStream or similar.

Combine this with Tommy DDD's answer, and i think you are set.

Upvotes: 1

t3dodson
t3dodson

Reputation: 4007

This isn't the most elegant solution but check out this answer. How can I determine if a file is binary or text in c#? you can psudo check for if the file is binary or text.

In the comments someone checked for 4 zero bytes in a row. \0\0\0\0 which tends to indicate binary file because we don't type NULL characters too often.

Upvotes: 0

Related Questions