Version 1.1 (December 2006) - open source - C# & NET 2.0

Dragging and dropping files from the Windows Explorer on your .NET application form or on a control, can be implemented easy with the DragEnter and DragDrop events. You can check if the dropped items are files (DataFormats.FileDrop) and convert them to an array of strings.

C# example:

private void FormFileDropTester_DragEnter(object sender, DragEventArgs e)
  if (e.Data.GetDataPresent(DataFormats.FileDrop))
    e.Effect = DragDropEffects.Copy;
    e.Effect = DragDropEffects.None;
private void FormFileDropTester_DragDrop(object sender, DragEventArgs e)
  string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
  foreach (string fileName in fileNames)

It becomes more complicated if you want to reuse this source code in other forms or when you want to set restrictions on the file extensions. Therefore I have written a small FileDrop component (C#, .NET 2.0). It captures some Windows messages (WM_DROPFILES) and calls Win32 API functions like DragQueryFile and DragFinish.

You only have to add the FileDrop component to your form, link a control or a form (DropControl), set the list of allowed file extensions (AllowedFileExtensions) and implement the FilesDropped event. You can also create a derived component and add some extra logic in the CheckAcceptFile function.

Class diagram:

FileDrop Class Diagram

C# example:

private void FormFileDropTester_Load(object sender, EventArgs e)
  fileDrop1.AllowDrop = true;
  fileDrop1.DropControl = listBox1;

private void fileDrop1_FilesDropped(object source, SCIPbe.Controls.FilesDroppedEventArgs e)
  listBox1.Items.Add("DropPoint.X = " +e.DropPoint.X.ToString());
  listBox1.Items.Add("DropPoint.Y = " + e.DropPoint.Y.ToString());
  listBox1.Items.Add("TotalFilesCount = " + e.TotalFilesCount.ToString());
  listBox1.Items.Add("AcceptedFilesCount = " + e.AcceptedFilesCount.ToString());

Copyrights and distribution

  • The component is open source (Mozilla Public License 1.1) and may be freely distributed.
  • The author doesn't give a warranty for error free running of this component and he doesn't give any support.
  • See source code for history and more information about the classes, interfaces, properties, methods, events, ...


FileDrop component
  • Contents: FileDrop component and demo program with sources (C#, .NET 2.0, Visual Studio 2005)
  • Version: 1.1 (2006-12-23)
  • File size: 107.3 Kb
  • Author: Stefan Cruysberghs
Download now