How can I determine that a particular file (which may or may not have an ".mp3" file extension) is in fact an MP3 file? I wish to do this in C#.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
C# code:
Files often start with a "magic number" to identify the data format. Depending on the format, a file starts with a certain sequence of bytes which is unique to that format. There's no standard to follow so it's not 100% reliable.
As fvu says, the mp3 magic number is
0x49 0x44 0x33
According to http://www.garykessler.net/library/file_sigs.html an mp3 file will always start with ID3 (hex 49 44 33) However, presence of these bytes only mean that the file is tagged with ID3 information. If this signature is not found, it could be an untagged mp3 file. To determine this, check out the mp3 file structure and you'll see that an mp3 frame starts with the signature ff fb (hex).
So :
49 44 33
or
ff fb
it's safe to assume that it's an MP3.