1

I would like to know how to Drag and Drop a folder and get its name. I already know how to do it with a file, but I'm not really sure how to modify it to be able to drag folders as well. Here's the code of the event that is triggered when a file is dropped:

private void checkedListBox_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        // NB: I'm only interested in the first file, even if there were
        // multiple files dropped
        string fileName = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
    }
}
Tuntuni
  • 481
  • 1
  • 7
  • 19
  • See also http://stackoverflow.com/questions/68598 – Robert Harvey Jun 27 '12 at 15:14
  • possible duplicate of [How do I distinguish a file or a folder in a drag and drop event in c#?](http://stackoverflow.com/questions/5893787/how-do-i-distinguish-a-file-or-a-folder-in-a-drag-and-drop-event-in-c) – user7116 Jun 27 '12 at 15:15
  • I have already googled. I'm not interested in basic drag and drop, but dropping a folder and getting its name, even if it's empty. EDIT: taking a look at sixlettervariables' link. – Tuntuni Jun 27 '12 at 15:16

1 Answers1

9

You could test if the path is a folder and in your DragEnter handler, conditionally change the Effect:

 void Target_DragEnter(object sender, DragEventArgs e)
 {
     DragDropEffects effects = DragDropEffects.None;
     if (e.Data.GetDataPresent(DataFormats.FileDrop))
     {
         var path = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
         if (Directory.Exists(path))
             effects = DragDropEffects.Copy;
     }

     e.Effect = effects;
 }
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
user7116
  • 63,008
  • 17
  • 141
  • 172