How do you check a file type when there is no extension in c#

Thomas07vt picture Thomas07vt · Mar 17, 2012 · Viewed 12.4k times · Source

How do you check a file type when there is no extension in c#

For instance, I have files with no extension, that are either .mp4 or .flv format (just no extension). I plan on converting these video files to audio files however I would like to determine the file type before I start converting it. Is there a way to do this in C#?

I was thinking that maybe I could just rename the file to name.mp4, then perform some task on the file that would either A) succeed, meaning that the file was indeed .mp4, or B) fail, in which case I could then rename it to .flv

then convert the file as the appropriate extension. Is there a native process in c# that can look at .mp4 properties or .flv properties? I do not want to rename the file to .mp4 and then open it in a third party application, such as Windows Media Player, in order to see if I named it correctly.

Answer

ToddBFisher picture ToddBFisher · Mar 17, 2012

I've heard of reading the first few bytes of a file's contents and making an educated guess at the file's format. This link seems promising:

Using .NET, how can you find the mime type of a file based on the file signature not the extension