You might have noticed that for some programs, you can drag files directly into them. This is a useful feature: it lets you save the time and effort required to open a file dialog box, and allows you to easily work with several files. For example, I just recently wrote a file renaming program, that can add/remove text from files that you drag onto it. Also, some programs let you drag a file onto its executable or shortcut. I'll show you how to enable both in your .NET programs.
Drag/Drop onto Windows Forms
The
Form class, along with several others, supports drag/drop. We'll assume that you are dragging/dropping directly to the form.
Start by setting the
AllowDrop property to true. You can do this in the properties window or in code:
Now that the form will accept dragging/dropping, we need some events to handle this. Let's add event handlers for DragEnter and DragDrop. You can do this in the properties window. Click the lightning bolt icon, and find the events. Double-click the blank space beside them.

Otherwise, you can do this in code. You will want to do this in the constructor after the InitializeComponent() call.
C# -
this.DragEnter += new DragEventHandler(Form1_DragEnter);
-
this.DragDrop += new DragEventHandler(Form1_DragDrop);
-
Now, let's fill in the events. We want our cursor to change to the drop cursor when we drag over the form. So in the
Form1_DragEnter event, put this code:
C# -
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
-
e.Effect = DragDropEffects.All;
-
This shows DragDrop effects if there is data available, and it is in the format of a file drop.
Now, we need to do something with the data when the file(s) are dropped. Here's the code to put into your
Form1_DragDrop event.
C# -
string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
-
foreach (string s in fileList)
-
{
-
//replace this with your own code
-
textBox1.Text += String.Format("{0}{1}", s, Environment.NewLine);
-
}
-
The data is a string array, but is returned as an object, so it must be typecasted. So
fileList is now holding the paths to the files that have been dropped. I've added the filenames to a TextBox, but you can do whatever you want with them.
System.IO.FileInfo is a very useful class for handling files. Now you can handle files dropped into your program.
Drag/Drop onto Shortcut/Executable Note: This doesn't work with shortcuts created by a setup package
You might want to be able to drag a file onto your desktop icon, and have the program start up and do something with that file. This is also possible.
When you drop a file onto an executable or it's shortcut, the path to that file is treated as an
command line argument. So, if I dragged the file C:\Dev\temp.txt onto text.exe, it would be the same as if I ran this from the command line:
DOS Command Line
Now you need to know how to get to these arguments. If you have made Command line programs, you may have noticed the parameter in your Main function:
C# -
static void Main(string[] args)
-
{
-
//your code
-
}
-
string[] args contains a list of the arguments. So, if we continue the previous example, args[0] would contain: @"c:\dev\temp.txt".
You can drop several files at once on the executable as well. These file paths will show up individually in the args array. So, you can loop through the args array to get all files dropped on your program.
But Windows Forms applications don't have this parameter by default. So how would you get that information to your Form? Well, you have to modify the Main method. Find your Main method in Program.cs:
C# -
static void Main();
-
//should become:
-
static void Main(string[] args)
-
Now you have access to the arguments, but you need to get them to your form. There is more than one way to do this, but I suggest modifying your constructor. Lets assume we are working with Form1. The old constructor looks like this:
C# -
public Form1()
-
{
-
InitializeComponent();
-
}
-
Change it to this:
C# -
public Form1(string[] args)
-
{
-
InitializeComponent();
-
}
-
Now your form will take a string array as a constructor parameter. Go back to Program.cs, and change the way you call Form1.
C# -
public Form1(string[] args)
-
{
-
Application.Run(new Form1());
-
//should become
-
Application.Run(new Form1(args));
-
}
-
Now you have access to your command line arguments in your form. You can store them, or do whatever you want with them.
Since the back and forth between the two pages can be a bit confusing, here's the full code for the program and the form.
Program.cs C# -
using System;
-
using System.Windows.Forms;
-
-
namespace DragDrop
-
{
-
static class Program
-
{
-
/// <summary>
-
/// The main entry point for the application.
-
/// </summary>
-
[STAThread]
-
static void Main(string[] args)
-
{
-
Application.EnableVisualStyles();
-
Application.SetCompatibleTextRenderingDefault(false);
-
Application.Run(new Form1(args));
-
}
-
}
-
}
-
Form1.cs C# -
using System;
-
using System.Windows.Forms;
-
-
namespace DragDrop
-
{
-
public partial class Form1 : Form
-
{
-
public Form1(string[] args)
-
{
-
InitializeComponent();
-
}
-
}
-
}