Drag and Drop Files with C#

Download DragDropFiles.zip

This tutorial will explain how to handle dragand drop events sent to one of the windows in your program by theWindows shell. In other words, this tutorial will teach you how toallow users to drag and drop files onto your form so you can do stuffwith them. This was bugging me for a little while, because it was sosimple in VB and for some reason it took me a while to figure it out inC#…anyway, on to the tutorial! This is the first tutorial I've everwritten, so bear with me :)

To start, create a new C# Windows Applicationproject called DragDropFiles. Put a ListBox control somewhere on theform. Set its AllowDrop property to "true". Click the Events lightningbolt to get to the events of the ListBox.

Now, to be able to handle files being dropped,at least two events need to be dealt with. The first is DragEnter,where basically all you have to do is allow the drag operation tocontinue. If you don't allow the operation to continue, say, becausethe user is dragging text into the control, the mouse cursor will staya "NO" symbol instead of becoming the "Copy Here" symbol (the pointerwith a "+" next to it).

The second event that must be handled(assuming they get past the DragEnter event) is DragDrop. This is whereyou actually read the files in the list and, in this application, addthem to a ListBox.

So, scroll down in the events list (make sureyou have the ListBox selected, not your form) until you find DragEnterand doubleclick it. Enter the following code in the listBox1_DragEntermethod:

// make sure they're actually dropping files (not text or anything else)
if( e.Data.GetDataPresent(DataFormats.FileDrop, false) == true )
// allow them to continue
// (without this, the cursor stays a "NO" symbol
e.Effect = DragDropEffects.All;

The comments should explain the code. Next, weneed to actually handle the drop. Get back to the graphical formediting interface and select the ListBox. Make sure you are viewing theEvents for the ListBox and scroll down to the DragDrop event anddoubleclick it. Enter the following code into the listBox1_DragDropmethod:

// transfer the filenames to a string array
// (yes, everything to the left of the "=" can be put in the
// foreach loop in place of "files", but this is easier to understand.)
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

// loop through the string array, adding each filename to the ListBox
foreach( string file in files )
{
listBox1.Items.Add(file);
}

You're done! That was simple :) Go run theprogram and drag and drop some files (My Computer and the Recycle Bindon't count as files) onto the ListBox and see what happens. It willadd the filename with FULL PATH to the ListBox, so if you want just thefilename you'll need to parse it out (something I won't talk abouthere). The example code shows this in action with everything done foryou, if you're that kind of person :) The example code alsodemonstrates how to grab the size of the file and put it into a label– something I was too lazy to explain here. Have fun, and happycoding!

Most Commented Articles :

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

2 Responses to “Drag and Drop Files with C#”

  1. Feels like Christmas! Thanks for the to the point explanation, much easier to complicate it myself than find the meat of a longer explanation.

    Not sure why some of the words in the text got stuck together though (for example “Go run theprogram” in the final paragraph.

    The example was not exactly what I needed (I just wanted one path into a text box. But it was so clear that I was able to modify it into what I needed as I typed and it worked. Just like a late Christmas present! Thanks again!

  2. Fantastic tutorial on incorporating the drag-n-drop feature into a C# project. This is exactly what I was looking for. Your example is very concise, easy to understand and to the point. Thank you!