问题
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.
回答1:
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
回答2:
I had played this utility (TrID - File Identifier) and seems quite accurate. File type defination package (TrIDDefs) is also up to date.
And Here is a list of file type signature table if you interest. The list is continuing work-in-process.
来源:https://stackoverflow.com/questions/9747278/how-do-you-check-a-file-type-when-there-is-no-extension-in-c-sharp