views:

272

answers:

2

I have a C# application that creates shortcuts to launch other programs with specific arguments and initial directories. I would like the user to be able to drag a shortcut from the Windows form and drop it anywhere relevant like the desktop, the start menu, and so on but I don't really know how to handle that, could anyone point me in the right direction?

I have seen a few samples using PInvoke and IShellLink like this one, or read answers on SO like here, which already help create shortcuts and save them in a .lnk file. I assume I have to hand over data in a DoDragDrop() call when the user initiates a drag operation, for example by handling a MouseDown signal. That's as far as I got, I suppose I need to know exactly which type the target is expecting to accept the drop, and how to serialize the shortcut, but couldn't find any information on that part.

Perhaps another option would be to get the location of the drop, and manage that from my application, but there again I'm a bit clueless as how to do that.

The framework version is currently 3.5, and I'm only considering Windows platforms.

Thanks in advance for your help!


Update/Solution:

Using the ShellLink code mentioned above to create a temporary shortcut file, I simply used DataObject for the drag and drop, like in the following example:

private void picShortcut_MouseDown(object sender, MouseEventArgs e)
{
    ShellLink link = new ShellLink();

    // Creates the shortcut:
    link.Target = txtTarget.Text;
    link.Arguments = txtArguments.Text;
    link.Description = txtDescription.Text;
    link.IconPath = txtIconFile.Text;
    link.IconIndex = (txtIconIndex.Text.Length > 0 ?
        System.Int32.Parse(txtIconIndex.Text) : 0);
    link.Save("tmp.lnk");

    // Starts the drag-and-drop operation:
    DataObject shortcut = new DataObject();
    StringCollection files = new StringCollection();
    files.Add(Path.GetFullPath("tmp.lnk"));
    shortcut.SetFileDropList(files);
    picShortcut.DoDragDrop(shortcut, DragDropEffects.Copy);
}

Quite complicated if you consider the PInvoke code (not shown here), and I still need to create this temporary file with the target name. If anyone knows a... erm, shortcut, it's welcome! Perhaps by porting the code for which John Knoeller gave a link (thanks!).

+1  A: 

I answered a question sort of similar to this on a previous thread. This might be a starting point for you.

Drag and Drop link

Chalkey
Thanks, but wouldn't that be to handle the drop part and create a shortcut from there? I'm looking for the other part. The `IWshShortcut` type isn't accepted by the Explorer, it seems, so I can't even save my shortcut under a temporary name and pass it to the `DoDragDrop()` method, but maybe I'm missing something?
RedGlyph
+3  A: 

Raymond Chen did a whole article on this very topic on his blog check out dragging a virtual file

John Knoeller
Interesting article. It mentions the IDataObject interface, after searching a bit on my own I found a way to drag and drop the shortcut from a temporary file, which I have to create anyway.
RedGlyph