开发者

When checking if a file is MP3 is checking the filename string with an .EndsWith good enough?

开发者 https://www.devze.com 2022-12-11 12:15 出处:网络
I\'m doing this: private void LoadSoundFile() { 开发者_Go百科if (openFileDialog1.ShowDialog() == DialogResult.OK)

I'm doing this:

    private void LoadSoundFile()
    {
 开发者_Go百科       if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            if (openFileDialog1.FileName.EndsWith(".mp3"))
            {
                txtFileName.Text = openFileDialog1.FileName;
            }
            else
            {
                MessageBox.Show("Currently Musicality only supports MP3 files.", "Unsupported file chosen.");                        
            }
        }

    }

Is there a better way of checking file types or am I doing it the right way?


Having the .mp3 extension doesn't mean it is an mp3, but not having it is an (acceptable) indication that it isn't.

At some point you will call some API to play the file, and it will fail. When it does, you know it's not a playable file. So make sure you handle that with some decent UI too.


Your question seems to ask if the right way to check if a file is MP3 is to look at the end of the filename. As others have said, the answer to that is no. Matt Warren's post can help you if you want to look into the file to see if it is actually mp3 format.

But your comment on Eran Betzalel's answer makes me wonder if you are asking generally whether the right way to check a file extension is to use String.EndsWith().

One thing to notice is that EndsWith(string) is case-sensitive, so the results of:

EndsWith("mp3")
EndsWith("Mp3")
EndsWith("MP3")

and

EndsWith("mP3")

don't all give the same answer. A better test might be:

if (Path.GetExtension(openFileDialog1.FileName).ToLower() == "mp3")

if all you care about is the file extension and not the file contents.


If you actually want to analyse the file (to check if it really is a .mp3) you'll need to look at the specification so you parse it correctly. Here is a good place to start and there is some more info here. This article on the CodeProject goes even further and extracts ID3 tags as well as the header.

This will be better than just checking that the extension is ".mp3", but it's a lot of extra work so it has to be worthwhile.


It really depends on the nature of your program. I think that if you are not developing a security related application, then you can use the simple extension check.


No, because file extension is simply an indicator, it is not a reliable guide to what the file is or contains.

I can name i music file as mySong.zzz and it will still play in Winamp. When you load it you should sample the start of the file to see if it really is an mp3.

You can also set a filter on your open file dialog so that it only allows the user to select mp3 files:

openFileDialog1.Filter = "mp3|*.mp3|All Files|*.*";


I guess the proper way to actually check if it's an MP3 file (this requires that the file be opened) is to look for "magic numbers", sequences of bytes within the binary data that always occur. In this case, you can use the ID3 tag's magic number: ID3v1 tags are stored in the last 128 bytes of the file starting with the bytes "TAG" (hexadecimal "544147"), while ID3v2 tags are stored at the beginning of the file, so the first 3 bytes of the file are "ID3" (hexadecimal "494433"). I don't know if the MP3 frames themselves have simple magic numbers like this. Obviously, this method requires the file to be opened, which could make a scan of a large number of files slower.


If you want to be sure, load the file with this lib http://sourceforge.net/projects/id3dotnet/ it will fail with an exception if not an mp3. Simply create an Id3.Net.Mp3File with filename or stream in constructor an see if it throws an exception

0

精彩评论

暂无评论...
验证码 换一张
取 消