Click here to Skip to main content
15,885,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a listbox in which I can drag and drop files, but I want my application to reject any file that is not either ".mp3", ".wav" or ".wma".

Tried using Path.GetExtension, but I don't know how to make it accept more than one type of file.

This is what I'm using:
C#
private void listBox1_DragDrop(object sender, DragEventArgs e)
       {
           if (e.Data.GetDataPresent(DataFormats.FileDrop))
           {
               string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop);

               foreach (string filename in filenames)
               {
                   if (Path.GetExtension(filename) == ".mp3")
                   {
                       listBox1.Items.Add(filename);
                   }
                   else
                   {
                       MessageBox.Show("This type of file is not supported!");
                   }

               }
           }
       }

So I want to make it function like the OpenFileDialog Filter option("*.mp3|*.wav|*.wma")

THNAKS IN ADVANCE!
Posted

1 solution

I think it's as simple as replacing:
C#
if (Path.GetExtension(filename) == ".mp3") {
    listBox1.Items.Add(filename);
}
else ...

with:
C#
string ext = Path.GetExtension(filename);
if ((ext == ".mp3") || (ext == ".wma") || (ext == ".wmv")) {
    listBox1.Items.Add(filename);
}
else ...
/ravi
 
Share this answer
 
Comments
ChrisCreateBoss 28-Jan-15 18:15pm    
Yeah! It works perfectly! Thank you Ravi!
Ravi Bhavnani 28-Jan-15 18:27pm    
You're welcome! :)

/ravi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900