views:

25

answers:

1

I want users to be able to drag elements from my program onto the desktop to create files from them. What's the simplest way to embed this information into a DataObject so explorer will accept drops and create the appropriate file(s)?

My experience with drag and drop is limited, and so far the only way I can think of is to actually create the files in a temporary directory and attach their paths to the DataObject via a DataFormat of "FileDrop"... but there must be a better way.

+1  A: 

Basically what I understand you want to do is drag a non-existent file from your application to explorer and have a physical file created.

Well, your solution of creating a temporary file and then setting that in the DataObject is probably the short cut solution.

The "correct" solution, without creating a temporary disk file will require significant amount of P/Invoke type interop to create a custom IDataObject that can render the file descriptor and content from memory rather than a physical source file. I have not done this in .NET, but I can guess it would be a fair amount of work.

Chris Taylor
I see. I guess the temp file is the simpler solution then... the real problem with this is that I don't really want to create a file for every drag operation, as there's no guarantee the user will drop the drag in explorer. That is, most of the time the drag will probably just be used to move data (i.e. text and images) between programs. Creating a file for every drag seems like a lot of waste, even if I could manage to clean it up in a reasonable manner...
chaiguy
If yu decide to give it a try, here is an article I just came across (http://www.codeproject.com/KB/tips/ExplorerDelayDrop.aspx), it is for managed code, but using 'System.Runtime.InteropServices.ComTypes.IDataObject' as a starting point I believe it could be achieved in managed code.
Chris Taylor