Click here to Skip to main content
15,881,709 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
20 Jan 2020CPOL2 min read 10.6K   8   2
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)


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
SuggestionWPF Pin
Jalapeno Bob22-Jan-20 8:22
professionalJalapeno Bob22-Jan-20 8:22 
PraiseNice Pin
dandy7220-Jan-20 8:03
dandy7220-Jan-20 8:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.