tags:

views:

185

answers:

7

I'm doing this:

    private void LoadSoundFile()
    {
        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?

+1  A: 

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|*.*";
slugster
Granted but not really what I was asking. +1 either way.
Sergio Tapia
Check my edit, may be more helpful :)
slugster
+1  A: 

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.

Eran Betzalel
That is my question! :D Is what I'm doing above an extension check, or is there another way I'm not aware of. Thanks.
Sergio Tapia
The only other way is to read the content of the file (as slugster claimed), which I think you don't need to.
Eran Betzalel
+4  A: 

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.

jeffamaphone
Might also want to do openFileDialog1.FileName.ToLower().EndsWith(".mp3")or use some kind of String comparison with an Ignore Case option.
Aaron
+2  A: 

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.

Matt Warren
A: 

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.

ascent1729
+2  A: 

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.

JeffH
A: 

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

Esben Skov Pedersen