Click here to Skip to main content
12,698,410 members (23,645 online)
Click here to Skip to main content
Add your own
alternative version


107 bookmarked

Drag and drop, cut/copy and paste files with Windows Explorer

, 9 May 2006
Rate this:
Please Sign up or sign in to vote.
This article describes how to implement file interaction with Windows Explorer

Drag and drop files into explorer


Recently, I needed to implement drag and drop with the Windows Explorer, but all of the examples I could find were not quite what I wanted. I wanted bi-directional, and in C#. So I wrote one.

This sample project lists a folder full of files, and lets you drag and drop them into Explorer. You can also drag from Explorer into the sample, and you can use the Shift and Ctrl keys to modify the action, just like in Explorer.

You can also use the right click options for cut and paste. I discovered that it is not possible to receive notifications when someone pastes your files into another folder (thereby cutting them out of the folder you are displaying) to allow you to update the view. So, I implemented a simple file watcher to watch the displayed folder and refresh it if it changes.


I found a couple of examples on MSDN for drag and drop, but not using files, but they were still useful (Performing Drag-and-Drop Operations and Control.DoDragDrop Method).

Also, there was a snippet for doing cut & paste, which was useful: How to Cut Files to the Clipboard Programmatically in C#?

Using the code

The sample is fairly self explanatory, I’ve tried to cut out any code that wasn’t showing stuff that was relevant.

To start a drag operation into Explorer, we implement the ItemDrag event from the Listview, which gets called after you drag an item more than a few pixels. We simply call DoDragDrop passing the files to be dragged wrapped in a DataObject. You don't really need to understand DataObject - it implements the IDataObject interface used in the communication.

/// <summary>
/// Called when we start dragging an item out of our listview
/// </summary>
private void listView1_ItemDrag(object sender, 
        System.Windows.Forms.ItemDragEventArgs e)
    string[] files = GetSelection();
    if(files != null)
        DoDragDrop(new DataObject(DataFormats.FileDrop, files), 
                   DragDropEffects.Copy | 
                   DragDropEffects.Move /* | 
                   DragDropEffects.Link */);

Then, during the drag operation, if the drag happens over your window, your DragOver method gets called. You have to tell the caller what would happen if the user drops on this location. You do this by setting e.Effect to the correct values:

/// <summary>
/// Called when someone drags something over our listview
/// </summary>
private void listView1_DragOver(object sender, 
        System.Windows.Forms.DragEventArgs e)
    // Determine whether file data exists in the drop data. If not, then
    // the drop effect reflects that the drop cannot occur.
    if (!e.Data.GetDataPresent(DataFormats.FileDrop)) 
        e.Effect = DragDropEffects.None;

    // Set the effect based upon the KeyState.
    if ((e.KeyState & SHIFT) == SHIFT && 
        (e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) 
        e.Effect = DragDropEffects.Move;

    else if ((e.KeyState & CTRL) == CTRL && 
        (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) 
        e.Effect = DragDropEffects.Copy;
    else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)  
        // By default, the drop action should be move, if allowed.
        e.Effect = DragDropEffects.Move;

        // Implement the rather strange behaviour of explorer that if the disk
        // is different, then default to a COPY operation
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
        if (files.Length > 0 && !files[0].ToUpper().StartsWith(homeDisk) && 
                                        // Probably better ways to do this
        (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) 
            e.Effect = DragDropEffects.Copy;
        e.Effect = DragDropEffects.None;

    // This is an example of how to get the item under the mouse
    Point pt = listView1.PointToClient(new Point(e.X, e.Y));
    ListViewItem itemUnder = listView1.GetItemAt(pt.X, pt.Y);

Then, when the user actually releases the mouse, the DragDrop method gets called. Here, we actually do the cut or copy operation:

/// <summary>
/// Somebody dropped something on our listview - perform the action
/// </summary>
private void listView1_DragDrop(object sender, 
        System.Windows.Forms.DragEventArgs e)
    // Can only drop files, so check
    if (!e.Data.GetDataPresent(DataFormats.FileDrop)) 

    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
    foreach (string file in files)
        string dest = homeFolder + "\\" + Path.GetFileName(file);
        bool isFolder = Directory.Exists(file);
        bool isFile = File.Exists(file);
        if (!isFolder && !isFile)
        // Ignore if it doesn't exist

                case DragDropEffects.Copy:
                    // TODO: Need to handle folders
                        File.Copy(file, dest, false);
                case DragDropEffects.Move:
                    if (isFile)
                        File.Move(file, dest);
                case DragDropEffects.Link:
                // TODO: Need to handle links
        catch(IOException ex)
            MessageBox.Show(this, "Failed to perform the" + 
              " specified operation:\n\n" + ex.Message, 
              "File operation failed", MessageBoxButtons.OK, 


That's all there is to it! Well, actually, there is a little more. If you want to use custom cursors, you can implement GiveFeedback, and if you want to know when the drag leaves your window, you can implement QueryContinueDrag, but I left these out to keep the code clean.

Cut and paste turned out to be quite simple as well. On the cut or copy operation, you just put the filenames on the clipboard using our trusty DataObject class:

/// <summary>
/// Write files to clipboard (from
///      archive/2005/10/27/10979.aspx)
/// </summary>
/// <param name="cut">True if cut, false if copy</param>
void CopyToClipboard(bool cut)
    string[] files = GetSelection();
    if(files != null)
        IDataObject data = new DataObject(DataFormats.FileDrop, files);
        MemoryStream memo = new MemoryStream(4);
        byte[] bytes = new byte[]{(byte)(cut ? 2 : 5), 0, 0, 0};
        memo.Write(bytes, 0, bytes.Length);
        data.SetData("Preferred DropEffect", memo);

And then on the paste (if someone pastes onto us), we do the reverse:

/// <summary>
/// Paste context menu option
/// </summary>
private void pasteMenuItem_Click(object sender, System.EventArgs e)
    IDataObject data = Clipboard.GetDataObject();
    if (!data.GetDataPresent(DataFormats.FileDrop))

    string[] files = (string[])
    MemoryStream stream = (MemoryStream)
      data.GetData("Preferred DropEffect", true);
    int flag = stream.ReadByte();
    if (flag != 2 && flag != 5)
    bool cut = (flag == 2);
    foreach (string file in files)
        string dest = homeFolder + "\\" + 
                File.Move(file, dest);
                File.Copy(file, dest, false);
        catch(IOException ex)
            MessageBox.Show(this, "Failed to perform the" + 
                " specified operation:\n\n" + ex.Message, 
                "File operation failed", 
                MessageBoxButtons.OK, MessageBoxIcon.Stop);


Points of interest

The most annoying thing was that if you cut from the sample and paste into Explorer, Explorer will move the files for you and take them out of your folder. However, there is no way to get a notification when this paste happens, so it is impossible to keep your view of the files up to date. I implemented a small file watcher which watches the sample folder, but I imagine this could get much more complicated if you need to watch many folders.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Paul Tallett
Web Developer
Europe Europe
I am a principal development consultant at Microsoft in the UK specialising in UI development. Recently I've been doing a lot of WPF work including the BBC iMP project shown at MIX06. I've been developing software for over 20 years - VAX, WIN16, MFC, ASP.NET, WinForms, WPF.

My main hobby is cars and my favourite day out is at Thruxton race track driving the Porsche 911 Turbo.

You may also be interested in...

Comments and Discussions

Questiondrag and drop default behaviour Pin
fredhimself21-May-16 0:04
memberfredhimself21-May-16 0:04 
QuestionOverwrite dialog goes behind Pin
Member 104559409-Dec-13 19:35
memberMember 104559409-Dec-13 19:35 
QuestionHow to change the behaviour of the drop event based on the target. Pin
extra_brain20-Apr-13 6:04
memberextra_brain20-Apr-13 6:04 
Questiongreat article thanks Pin
dheerajv5-Jul-12 23:46
memberdheerajv5-Jul-12 23:46 
GeneralMaking it work in Windows 7 Pin
DanielRose198112-Jul-10 2:59
memberDanielRose198112-Jul-10 2:59 
GeneralRe: Making it work in Windows 7 Pin
Pandele Florin18-Apr-11 8:26
memberPandele Florin18-Apr-11 8:26 
QuestionHow to paste standard file to the disk from Clipboard Pin
gihanatt15-Apr-10 14:39
membergihanatt15-Apr-10 14:39 
QuestionNeed help Pin
kayru31-Mar-10 1:18
memberkayru31-Mar-10 1:18 
GeneralAwesome App Pin
noka0327-Nov-09 18:31
membernoka0327-Nov-09 18:31 
GeneralWondering how to do this... Pin
n13ldo15-Aug-09 4:09
membern13ldo15-Aug-09 4:09 
GeneralRe: Wondering how to do this... Pin
Xmen W.K.16-Mar-12 5:39
memberXmen W.K.16-Mar-12 5:39 
QuestionHow to WPF? Pin
wetsnow12-Feb-09 9:21
memberwetsnow12-Feb-09 9:21 
GeneralThanks for this Pin
Rick Hansen25-Aug-08 11:45
memberRick Hansen25-Aug-08 11:45 
Answerto refresh the list after cut Pin
asdfwefwef14-Jul-08 7:26
memberasdfwefwef14-Jul-08 7:26 
GeneralDrag Drop Pin
KaurGurpreet21-Nov-07 1:05
memberKaurGurpreet21-Nov-07 1:05 
GeneralVery useful Pin
srelu24-Oct-07 0:34
membersrelu24-Oct-07 0:34 
GeneralRe: Very useful Pin
JoachimBlaurock11-Feb-08 1:31
memberJoachimBlaurock11-Feb-08 1:31 
Generaljust make control droppable area Pin
viki3d2-Jul-07 10:01
memberviki3d2-Jul-07 10:01 
GeneralSame folder problem Pin
Zeeshan Shigri26-May-07 6:16
memberZeeshan Shigri26-May-07 6:16 
AnswerRe: Same folder problem Pin
Aurican31-Aug-07 13:46
memberAurican31-Aug-07 13:46 
GeneralRe: Same folder problem Pin
srelu24-Oct-07 0:53
membersrelu24-Oct-07 0:53 
GeneralRe: Same folder problem Pin
Zeeshan Shigri24-Oct-07 1:08
memberZeeshan Shigri24-Oct-07 1:08 
GeneralCopy Paste in windows application Pin
balamurugan8216-Apr-07 8:36
memberbalamurugan8216-Apr-07 8:36 
GeneralRe: Copy Paste in windows application Pin
srelu24-Oct-07 1:27
membersrelu24-Oct-07 1:27 
GeneralSource file location is uknown Pin
vmo3d21-Feb-07 15:03
membervmo3d21-Feb-07 15:03 
AnswerRe: Source file location is uknown Pin
Aurican31-Aug-07 13:42
memberAurican31-Aug-07 13:42 
QuestionDrawing Line With Drag And Drop Pin
mminouei31-Oct-06 7:03
membermminouei31-Oct-06 7:03 
GeneralDrawing Line With Drag And Drop Pin
mminouei28-Oct-06 11:30
membermminouei28-Oct-06 11:30 
GeneralDragDropEffects.Link [modified] Pin
mminouei24-Oct-06 11:43
membermminouei24-Oct-06 11:43 
GeneralRe: DragDropEffects.Link Pin
Paul Tallett24-Oct-06 23:55
memberPaul Tallett24-Oct-06 23:55 
Generalshow icons instead of text Pin
criscoff11-Aug-06 3:24
membercriscoff11-Aug-06 3:24 
AnswerRe: show icons instead of text Pin
Toadflakz_UK4-May-07 2:01
memberToadflakz_UK4-May-07 2:01 
Questiondo drag an drop just whin cklic on icon Pin
syriast31-Jul-06 2:31
membersyriast31-Jul-06 2:31 
GeneralNice job Pin
Bnaya Eshet24-Jul-06 6:36
memberBnaya Eshet24-Jul-06 6:36 
GeneralGetData always returns null Pin
mork57117-Jul-06 20:06
membermork57117-Jul-06 20:06 
GeneralRe: GetData always returns null Pin
Paul Tallett22-Jul-06 6:03
memberPaul Tallett22-Jul-06 6:03 
GeneralRe: GetData always returns null Pin
srelu24-Oct-07 2:16
membersrelu24-Oct-07 2:16 
GeneralDrag Drop Images Pin
J.S.Lim8-Jul-06 17:07
memberJ.S.Lim8-Jul-06 17:07 
GeneralRe: Drag Drop Images Pin
Paul Tallett22-Jul-06 6:06
memberPaul Tallett22-Jul-06 6:06 
GeneralRe: Drag Drop Images Pin
srelu24-Oct-07 17:20
membersrelu24-Oct-07 17:20 
GeneralWatch for renamed files Pin
Sondre Kjell Selnes31-May-06 12:40
memberSondre Kjell Selnes31-May-06 12:40 
QuestionDrag and drop of special folders Pin
nagarsoft10-May-06 0:59
membernagarsoft10-May-06 0:59 
AnswerRe: Drag and drop of special folders Pin
Paul Tallett11-May-06 10:59
memberPaul Tallett11-May-06 10:59 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 9 May 2006
Article Copyright 2006 by Paul Tallett
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid