Drag And Drop Files to a C# Application

June 11, 2009 | By

The .NET Framework makes it easy to detect objects dragged and/or dropped into a Windows Forms application using one or more of the drag-and-drop events available. In these events you could check if the object is a file.


To enable a control to be a target of a drag-and-drop operation set its AllowDrop property to true and use one or more of the following events:

  • DragEnter: Occurs when the user drags an object into the control’s area, and has not released the mouse button yet.
  • DragOver: Occurs when the object is being dragged in the control’s area.
  • DragDrop: Occurs when the user lets go of the dragged object in the control’s area.
  • DragLeave: Occurs if the object dragged into the control is dragged out again without the user releasing the mouse button, or the user canceled the operation by pressing the Escape key.
  • GiveFeedback: Occurs when the drag-and-drop operation starts and is used to modify the visual feedback of the operation.
  • QueryContinueDrag: Occurs when the keyboard or mouse state is changed during the drag-and-drop operation.

To process one or more files dragged and dropped into a control two events are needed, DragEnter and DragDrop. The if statement in DragEnter checks what is dragged in is of type DataFormats.FileDrop, the Windows file drop format, or not. If true the drag operation is allowed. The DragDrop event retrieves the list of files dropped using the GetData method and casts them to an array of strings. Each element of the array will contain a full path of one of the files dropped.


Download Source Code


Filed in: WinForms | Tags:

Comments (8)

  1. I always have trouble getting the drag-and-drop to behave properly. It’s one of those things that should be simpler than it is, IMO. Thanks for the example

  2. Thank you , very useful

  3. Bipin

    It was very helpful. Thanks.

  4. Kevin

    Very nice example.

    One suggestion: if( (…) == true ) { … } seems a bit of overkill to me. When (…) is true, one doesn’t usually check for it to be true. Off-course, some people like the explicit-icy.

    Thanks for sharing this drag & drop example.

  5. You’re welcome guys.

    @Kevin that was only to differentiate between the false in GetDataPresent()’s arguments and its return value.

  6. Kumar Saurabh

    One of the best example.

  7. Jadav Vishal

    Good job..
    This is really helpful to me..

  8. MirceaDrac

    void contentPanel_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

    foreach (string file in files)
    if (Directory.Exists(file))

    void AddDirectoryFromPath(string folderPath)
    string[] files = Directory.GetFiles(folderPath);
    foreach (string file in files)
    string[] folders = Directory.GetDirectories(folderPath);
    foreach (string folder in folders)