I am attempting to drag a file represented by a TreeView node onto the desktop, Windows Explorer, or any other applications that allow files to be dropped onto them. I've written the code below based on various Internet examples I've found and I'm running it as administrator. It does allow me to drag as long as I remain in the TreeView control that contains the nodes, displaying the Copy icon with the cursor as it moves. However, when I drag it off the control to the desktop or Windows Explorer for example, the icon turns into the red circle with a slash across it and nothing gets dropped. I've ensured that the dragged file actually does exist.
private void treeView_Files_ItemDrag(object sender, ItemDragEventArgs e)
{
DoDragDrop(e.Item, DragDropEffects.Copy);
}
private void treeView_Files_DragDrop(object sender, DragEventArgs e)
{
TreeNode NewNode;
if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
{
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);
NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
string[] files = new string[] { "C:\\temp\\TestFile.pdf" };
DataObject dataObject = new DataObject(DataFormats.FileDrop, files);
DoDragDrop(dataObject, DragDropEffects.Copy);
}
}