views:

43

answers:

2
  1. How to put a cut/copy reference to specific files and/or folders into Windows clipboard so that when I open standard Windows Explorer window, go to somewhere and press Ctrl+V - the files are pasted?

  2. If I copy or cut some files/folders in Windows Explorer, how do I get this info (full names and whether they were cut or copied) in my Program?

I program in C#4, but other languages ways are also interesting to know.

+1  A: 

If you're using Winforms, look at System.Windows.Forms.Clipboard, I think that should be able to do that. I'm not sure how to do what you want since I've never looked in to it, but I'd look at the FileDropList methods ('GetFileDropList' etc) first since they look promising.

http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.getfiledroplist%28v=VS.100%29.aspx

Edit: If you need to find out if it was a copy or a cut and similar more detailed info it seems like you'll have to use the IDataObject interface:

http://msdn.microsoft.com/en-us/library/system.windows.forms.idataobject.aspx

ho1
+3  A: 

I've got the 90% solution, reverse-engineered from the clipboard formats and my answer in this thread. You'll need to set two pieces of clipboard data. The list of files, that's easy to do. And another clipboard format named "Preferred Dropeffect" that indicates whether a copy or a move of the files is requested. Leading to this code:

    public static void StartCopyFiles(IList<string> files, bool copy) {
        var obj = new DataObject();
        // File list first
        var coll = new System.Collections.Specialized.StringCollection();
        coll.AddRange(files.ToArray());
        obj.SetFileDropList(coll);
        // Then the operation
        var strm = new System.IO.MemoryStream();
        strm.WriteByte(copy ? (byte)DragDropEffects.Copy : (byte)DragDropEffects.Move);
        obj.SetData("Preferred Dropeffect", strm);
        Clipboard.SetDataObject(obj);
    }

Sample usage:

        var files = new List<string>() { @"c:\temp\test1.txt", @"c:\temp\test2.txt" };
        StartCopyFiles(files, true);

Pressing Ctrl+V in Windows Explorer copied the files from my c:\temp directory.

What I could not get going is the "cut" operation, passing false to StartCopyFiles() produced a copy operation, the original files where not removed from the source directory. No idea why, should have worked. I reckon that the actual stream format of "Preferred DropEffects" is fancier, probably involving the infamous PIDLs.

Hans Passant