Click here to Skip to main content
12,950,832 members (57,092 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 22 Jan 2009

Setting the Clipboard File DropList with DropEffect in VB.Net

, 22 Jan 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
How you Set the File Drop Effect so you can cut files from VB.Net to the file explorer


In VB6, it is possible to use your app to put a list of files on the windows clipboard along with data telling if you want to cut or copy those files. Then the Windows file explorer can then take that list and copy or move those files. A lot of API calls are used to accomplish this.

In VB.Net, it is also possible to put this list of files, called a file drop list, on the clipboard as well as the DropEffect which tells Windows File explorer if the file list should be moved (a cut) or copied. You do not need all the API calls that were used in VB6 to accomplish it.

Set DropEffect in VB6:

In VB6 the file droplist is set with API calls to OpenClipboard, CopyMem, and SetClipboardData. The drop Effect is set with MoveMemory and RegisterClipboardFormat. Below is the function that is called in VB6 to set the drop effect. It is called with a vbDropEffectCopy to request a copy, and a vbDropEffectMove to request a cut.

Public Function SetFilesDropEffect(ByVal lEffect As Long) As Boolean
    Dim lMemoryHandle As Long
    Dim lMemoryPointer As Long
    Dim lFormat As Long
    Dim lReturn As Long
    ' Open the clipboard.
    If OpenClipboard(0) Then
        ' Get global memory pointer to hold the lEffect variable
        lMemoryHandle = GlobalAlloc(GHND, Len(lEffect))
        ' If we have a memory handle
        If lMemoryHandle Then
            ' Lock the memory while we initialize it.
            lMemoryPointer = GlobalLock(lMemoryHandle)
            ' Copy the lEffect variable into the global memory.
            Call MoveMemory(ByVal lMemoryPointer, lEffect, Len(lEffect))
            ' Unlock the memory
            Call GlobalUnlock(lMemoryHandle)
            ' Register the format
            lFormat = RegisterClipboardFormat(CFSTR_PREFERREDDROPEFFECT)
            ' Copy the format to the clipboard.
            lReturn = SetClipboardData(lFormat, lMemoryHandle)
            ' Check if we succeed
            If lReturn Then
                SetFilesDropEffect = True
            End If
        End If
        ' Close the clipboard.
        Call CloseClipboard
    End If
End Function

Set FileDropList and DropEffect in VB.Net:

In VB.Net, the Clipboard.SetFileDropList function can be used to place a list of files on the clipboard. It does not however have any way to set the DropEffect to tell File Explorer that you want to cut files instead of copy. So everything you put on the clip board this way is a copy.

If you use the SetDataObject to write a correctly created DataObject to the clipboard, you can pass the DropEffect as well as the DropFile List.

Setting the DropEffect in VB.Net requires creating a Data Object and writing it to the clipboard with Clipboard.SetDataObject. The Data Object needs to contain both the File DropList and DropEffect. They are put into the Object with the SetData method.

1) You need a String array of the filenames. It needs to be trimmed so there are no trailing spaces.
2) The DropEffectData is a 4 byte long byte array. The first byte is either 2 for move or 5 for copy. The remaining bytes are zeros.
3) Turn the DropEffectData into a 4 byte memory stream. This is the format of the Preferred Drop Effect data.
4) Create the Data Object.
5) Using SetData write the File DropList and DropEffect memory stream to the Data Object.
6) Clear the clipboard (this isn't required).
7) Write the data object to the clipboard with SetDataObject.

Imports System.IO

        '*>Set Drop File Names in String Array
        Dim sFiles() As String = Split(RichTextBox1.Text.TrimEnd, vbCrLf)

        '*>Set Drop File Effect in Memory Stream
        Dim DropEffectData(3) As Byte
        Select Case TextBox1.Text.Substring(0, 1).ToString
            Case 2 'Move
                DropEffectData(0) = 2
            Case 5 'Copy
                DropEffectData(0) = 5
            Case Else 'Copy
                DropEffectData(0) = 5
        End Select
        DropEffectData(1) = 0
        DropEffectData(2) = 0
        DropEffectData(3) = 0

        '*>Create Memory Stream From DropEffectData
        Dim mstream As Stream = New MemoryStream(4)
        mstream.Write(DropEffectData, 0, DropEffectData.Length)

        '*>Create Data Object with DropList and DropEffect
        Dim data_object As New DataObject()
        data_object.SetData("FileDrop", True, sFiles)
        data_object.SetData("Preferred DropEffect", mstream)

        '*>Clear Clipboard

        '*>Put Data Object on Clipboard
        Clipboard.SetDataObject(data_object, True)

Sample App

The Sample app will read the clipboard. If it finds a File DropList (from a File Explorer cut or paste), it will display the contents and DropEffect. The Set File DropList button will then Enable.

Clicking the Set File Droplist will execute the code above which Copies the data from the form and puts it back on the clipboard. You can then finish the cut or paste back in Explorer.


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


About the Author

Scott Bright
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMove several files doesn't work [modified] Pin
liquidheat3-Jul-09 5:50
memberliquidheat3-Jul-09 5:50 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 22 Jan 2009
Article Copyright 2009 by Scott Bright
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid