Detect when Paste file/folder is used in Windows Explorer

leorob88

Well-known member
Joined
Dec 31, 2020
Messages
73
Programming Experience
1-3
I'm trying to create a context menu to handle files/folders. I implemented this method to cut or copy files.

C#:
Expand Collapse Copy
private void cutcopy(string name, bool cut)
        {

            StringCollection file = new StringCollection() { name };
            MemoryStream stream = new MemoryStream();
            stream.WriteByte(cut ? (byte)2: (byte)5);
            DataObject data = new DataObject();
            data.SetFileDropList(file);
            data.SetData("Preferred DropEffect", stream);
            Clipboard.SetDataObject(data, true);
        }

the copy feature seems integrated fine with windows explorer. but when i use cut, then pasting through Explorer does indeed copy the file but does not delete/remove the source file. i couldn't find a proper clear solution.
 
It is your responsibility to delete the source when you respond to a Cut operation. See the Windows documentation regarding the clipboard:


If you don't want to delete immediately when you get the cut operation, then you will need to learn more about delayed rendering for the clipboard. From the 20,000 ft level delayed rendering is when you offer a set of formats on the clipboard, but don't actually provide the data. Later when the data format is requested, then you actually provide the data, and in the case of the cut operation, delete the source.
 
It is your responsibility to delete the source when you respond to a Cut operation. See the Windows documentation regarding the clipboard:


If you don't want to delete immediately when you get the cut operation, then you will need to learn more about delayed rendering for the clipboard. From the 20,000 ft level delayed rendering is when you offer a set of formats on the clipboard, but don't actually provide the data. Later when the data format is requested, then you actually provide the data, and in the case of the cut operation, delete the source.
Oh, ok. I find it curious such a simple operation is handled with such complexity but I will try to learn more about it!
 
Stop and think about it. Let's say your application stores files in a encrypted database rather a plain old file living on the filesystem. When you offer up the file in the clipboard as part of a cut or move operation, and another program accepts that format, how will that program know how to decrypt your database, find the file, and delete it from the database? It's your job as the source application to effect the delete if you want to offer cut or move.

WinForms tries to hide a lot of the complexities of the Win32 API. MS's original goal at the time was to hit that balance between attracting the VB6 programmers, the MFC programmers, and the IT folks who just do plain old line-of-business internal applications within a company. They were figuring that if you are hardcore Win32 API programmer, you would continue using C/C++ to access the Win32 APIs, or you would know how to read the Win32 API documentation and the WinForms documentation to know how to override the default behavior of WinForms to act more like a classic Win32 API program.
 
Back
Top Bottom