Sergio Tapia
Sergio Tapia

Reputation: 41228

How can I drag a folder from my Windows Explorer into a ListView and load the files into it?

I can't seem to make this work using this:

private void listView1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effect = DragDropEffects.Copy;
        }
    }

    private void listView1_DragDrop(object sender, DragEventArgs e)
    {
        string[] directoryName = (string[])e.Data.GetData(DataFormats.FileDrop);
        string[] files = Directory.GetFiles(directoryName[0]);

        foreach (string file in files)
        {
            if (Path.GetExtension(file) == ".mp3")
            {
                listView1.Items.Add(file);
            }
        }
    }

The mouse cursor shows a NOT sign and I can't drop the folder in my program.

Upvotes: 0

Views: 1697

Answers (1)

CoderDennis
CoderDennis

Reputation: 13837

Have you set the AllowDrop property of your ListView to True?

Is your DragEnter event ever being hit?

Upvotes: 3

Related Questions