281

I've seen this done in Borland's Turbo C++ environment, but I'm not sure how to go about it for a C# application I'm working on. Are there best practices or gotchas to look out for?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

10 Answers10

548

Some sample code:

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      this.AllowDrop = true;
      this.DragEnter += new DragEventHandler(Form1_DragEnter);
      this.DragDrop += new DragEventHandler(Form1_DragDrop);
    }

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

    void Form1_DragDrop(object sender, DragEventArgs e) {
      string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
      foreach (string file in files) Console.WriteLine(file);
    }
  }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 65
    Disclaimer : it may not work in debug if you run Visual Studio as an admin in Windows 7, or if you run your program as an admin. See [here](http://stackoverflow.com/questions/2833709/c-sharp-drag-drop-does-not-work-on-windows-7) – Matthieu Mar 22 '12 at 16:06
  • 4
    @Burnsys if you have the filepath from the drag operation, then you can read using `io.File` – Smith May 24 '13 at 23:51
  • Shouldn't the class be a sealed class to avoid making a virtual call on the `this.AllowDrop`? http://stackoverflow.com/questions/119506/virtual-member-call-in-a-constructor – CJBS Feb 10 '14 at 22:43
  • 1
    Ugh, no. Set the AllowDrop property to True in the designer and reason it out from there. – Hans Passant Feb 10 '14 at 23:02
  • 3
    Is the `(string[])` cast safe for any `FileDrop`-formatted drop? That is, is it possible to generate a `FileDrop` that will cause an illegal cast exception to `string[]`? I'm having trouble figuring that out from [the docs](https://msdn.microsoft.com/en-us/library/c6a9edsb(v=vs.110).aspx). – kdbanman Aug 11 '15 at 19:54
  • Great code snippet. I had a program that opened the files using dialog, and it was easy to add this feature in, and to make the adjustments so it worked just like dialog open but with drag and drop. – Caperneoignis Jul 13 '16 at 16:48
  • Works in WPF, too except the DragDrop event is named "Drop" – John Melville Aug 03 '17 at 23:33
  • What is the difference between `Enter` and `Drop` here? – DjangoBlockchain Mar 01 '18 at 07:19
  • @Childishforlife Enter is when you hold the mouse button and Enter it into control, still holding and Drop is the moment when you let the mouse button go. – Dominik Szymański Sep 05 '18 at 16:26
160

Be aware of windows vista/windows 7 security rights - if you are running Visual Studio as administrator, you will not be able to drag files from a non-administrator explorer window into your program when you run it from within visual studio. The drag related events will not even fire!

starball
  • 20,030
  • 7
  • 43
  • 238
Wayne Uroda
  • 5,025
  • 4
  • 30
  • 35
  • 7
    @Wayne Uroda: I thought my code wasn't working - heck it was giving me a big "No symbol" like this http://en.wikipedia.org/wiki/File:ProhibitionSign2.svg . Then I saw this answer and ran VS as a non-admin and presto it works! Thanks a million. – Derek W Jun 03 '13 at 18:09
  • 1
    Can't thank you enough for this, I would have given up unless I happened to find this post! It's as valid in Windows 10 in 2017 as it was when you wrote it. – Culme Jan 19 '17 at 10:57
  • Ok but what can we do? It is illogical? What if my program needs admin rights? – Meric Ozcan May 08 '22 at 09:27
  • @MericOzcan does this help https://stackoverflow.com/questions/2833709/c-sharp-drag-drop-does-not-work-on-windows-7 ? – Wayne Uroda May 09 '22 at 14:53
  • @WayneUroda I changed my program, now it selects files from path. – Meric Ozcan May 28 '22 at 19:52
51

In Windows Forms, set the control's AllowDrop property, then listen for DragEnter event and DragDrop event.

When the DragEnter event fires, set the argument's AllowedEffect to something other than none (e.g. e.Effect = DragDropEffects.Move).

When the DragDrop event fires, you'll get a list of strings. Each string is the full path to the file being dropped.

The1nk
  • 702
  • 14
  • 25
Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
17

You need to be aware of a gotcha. Any class that you pass around as the DataObject in the drag/drop operation has to be Serializable. So if you try and pass an object, and it is not working, ensure it can be serialized as that is almost certainly the problem. This has caught me out a couple of times!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Phil Wright
  • 22,580
  • 14
  • 83
  • 137
14

Yet another gotcha:

The framework code that calls the Drag-events swallow all exceptions. You might think your event code is running smoothly, while it is gushing exceptions all over the place. You can't see them because the framework steals them.

That's why I always put a try/catch in these event handlers, just so I know if they throw any exceptions. I usually put a Debugger.Break(); in the catch part.

Before release, after testing, if everything seems to behave, I remove or replace these with real exception handling.

Guge
  • 4,569
  • 4
  • 35
  • 47
10

Here is something I used to drop files and/or folders full of files. In my case I was filtering for *.dwg files only and chose to include all subfolders.

fileList is an IEnumerable or similar In my case was bound to a WPF control...

var fileList = (IList)FileList.ItemsSource;

See https://stackoverflow.com/a/19954958/492 for details of that trick.

The drop Handler ...

  private void FileList_OnDrop(object sender, DragEventArgs e)
  {
    var dropped = ((string[])e.Data.GetData(DataFormats.FileDrop));
    var files = dropped.ToList();

    if (!files.Any())
      return;

    foreach (string drop in dropped)
      if (Directory.Exists(drop))
        files.AddRange(Directory.GetFiles(drop, "*.dwg", SearchOption.AllDirectories));

    foreach (string file in files)
    {
      if (!fileList.Contains(file) && file.ToLower().EndsWith(".dwg"))
        fileList.Add(file);
    }
  }
Community
  • 1
  • 1
CAD bloke
  • 8,578
  • 7
  • 65
  • 114
9

Another common gotcha is thinking you can ignore the Form DragOver (or DragEnter) events. I typically use the Form's DragOver event to set the AllowedEffect, and then a specific control's DragDrop event to handle the dropped data.

Craig Eddy
  • 889
  • 1
  • 6
  • 17
4

The solution of Judah Himango and Hans Passant is available in the Designer (I am currently using VS2015):

enter image description here

enter image description here

Roland
  • 4,619
  • 7
  • 49
  • 81
0

You can implement Drag&Drop in WinForms and WPF.

  • WinForm (Drag from app window)

You should add mousemove event:

private void YourElementControl_MouseMove(object sender, MouseEventArgs e)

    {
     ...
         if (e.Button == MouseButtons.Left)
         {
                 DoDragDrop(new DataObject(DataFormats.FileDrop, new string[] { PathToFirstFile,PathToTheNextOne }), DragDropEffects.Move);
         }
     ...
    }
  • WinForm (Drag to app window)

You should add DragDrop event:

private void YourElementControl_DragDrop(object sender, DragEventArgs e)

    {
       ...
       foreach (string path in (string[])e.Data.GetData(DataFormats.FileDrop))
            {
                File.Copy(path, DirPath + Path.GetFileName(path));
            }
       ...
    }

Source with full code.

0

Note that for this to work, you also need to set the dragDropEffect within _drawEnter...

private void Form1_DragEnter(object sender, DragEventArgs e)
{
    Console.WriteLine("DragEnter!");
    e.Effect = DragDropEffects.Copy;
}

Source: Drag and Drop not working in C# Winforms Application

Jack
  • 871
  • 1
  • 9
  • 17