Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Allowing Explorer to Drop Files on a WinForms App, and Doing Something with Them

5.00/5 (2 votes)
20 Jan 2020CPOL2 min read 12.4K  
It's pretty easy to let Windows Explorer drop files on a WinForms app, but I can never remember how. So since I needed a file touch app, I thought I'd document the process here.

Introduction

Herself likes Jigsaws, but so does the cat - so she does them on her tablet where the cat can't smash them, run off an bury a couple of pieces ... you know how it goes.

But this means she goes through images like nobodies business. And who gets to find them? Oh yes. "Cat Picture Man", that's me. Find 'em, trim 'em to square, save 'em where she can get at them (her preferred app only works with square images, none of these fancy "rectangles" for it, oh no).

And she can't remember what pictures she has or hasn't done yet, so they need to be filed in a sortable manner so she knows to look at "just the new ones".

Background

So the obvious thing to do is to use the modification date, which Android Gallery (her preferred "find a file" method) supports.

But ... I prefer to "batch convert" the images, so I've got bunches available when she runs out and screams for "MORE KITTY PICTURES!".

So, I need a File Touch app: I can update the mod date of the latest dozen I give her, and they are all "grouped together" in Gallery. Perfect. And I'm lazy, so I'd rather pick the batch in Explorer, drop 'em on an app, and have it "Touch" them.

Which means Drag'n'Drop - which is easy to do, but I can never remember how exactly to do it. So here it is, where I can - hopefully - find it next time...

Using the Code

  1. Set the AllowDrop property of your form to True.
  2. Create a handler for both the DragEnter and DragDrop events of your form.
  3. In the DragEnter handler, add the following line of code:
    C#
    e.Effect = DragDropEffects.Move;
    
  4. In the DragDrop handler, add the following:
    C#
    string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop);
    if (files != null)
        {
        foreach (string file in files)
            {
            Console.WriteLine(file); // Or whatever you need to do...
            }
        }
    

In my case, the code I need is:

C#
private void FrmMain_DragEnter(object sender, DragEventArgs e)
    {
    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
    if (files != null)
        {
        e.Effect = DragDropEffects.Copy;
        }
    }

private void FrmMain_DragDrop(object sender, DragEventArgs e)
    {
    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
    if (files != null)
        {
        DateTime now = DateTime.UtcNow;
        tbResults.Text = "";
        foreach (string file in files)
            {
            try
                {
                File.SetCreationTimeUtc(file, now);
                File.SetLastWriteTimeUtc(file, now);
                File.SetLastAccessTimeUtc(file, now);
                tbResults.AppendText($"Touched \"{Path.GetFileName(file)}\"");
                }
            catch (Exception ex)
                {
                tbResults.AppendText($"Unable to touch \"{Path.GetFileName(file)}\"");
                }
            tbResults.AppendText(Environment.NewLine);
            }
        }
    }

And, Just for Completeness ...

How do you supply files to another app? That's not hard either.

It's a little more complex, as you need to use a control that supports dragging, such as the ListView.

Drop a control on your form (no control, no drag source - you can't drag directly from the surface of a Form). Handle the control's ItemDrag event.

In this case, a ListView:

C#
private void myListView_ItemDrag(object sender, ItemDragEventArgs e)
    {
    StringCollection sc = new StringCollection();
    sc.Add(@"D:\Test Data\MyPic.jpg");
    DataObject do = new DataObject();
    do.SetFileDropList(sc);
    myListView.DoDragDrop(do, DragDropEffects.Copy);
    }

History

  • 2020-02-20: Original version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)