private void testSetNavigationTree_DragDrop( object sender, DragEventArgs e )
{
// Retrieve the client coordinates of the drop location.
Point targetPoint = testSetNavigationTree.PointToClient( new Point( e.X, e.Y ) );
// Retrieve the node at the drop location.
TreeNode targetNode = testSetNavigationTree.GetNodeAt( targetPoint );
// Retrieve the node that was dragged.
var draggedNode = (TreeNode) e.Data.GetData( typeof (TreeNode) );
// Confirm that the node at the drop location is not
// the dragged node and that target node isn't null
// (for example if you drag outside the control)
if (!draggedNode.Equals( targetNode ) && targetNode != null)
{
//--------------------------------//
//--- Physically move the file ---//
//--------------------------------//
var tag = targetNode.Tag as DirectoryInfo;
if (tag != null)
{
if (draggedNode.Tag is FileInfo)
{
bool okToDrop = true;
string targetFileName = Path.Combine( tag.FullName,
( (FileInfo) draggedNode.Tag ).Name );
bool targetExists = File.Exists( targetFileName );
if (targetExists)
{
string message = "File \"{0}\" aready exists, would you like to overwrite it?";
okToDrop = DialogResult.Yes == MessageBox.Show( string.Format( message, targetFileName ),
@"V E R I F Y",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question );
}
if (okToDrop)
{
var fi = new FileInfo( targetFileName );
if (fi.Exists)
{
fi.IsReadOnly = false;
fi.Delete();
}
File.Move( ( (FileInfo) draggedNode.Tag ).FullName, targetFileName );
draggedNode.Tag = new FileInfo( targetFileName );
// Remove the node from its current
// location and add it to the node at the drop location.
draggedNode.Remove();
//targetNode.Nodes.Add( draggedNode );
// Expand the node at the location
// to show the dropped node.
targetNode.Expand();
//Reload tree for now to remove existing node for same file
if (targetExists)
LoadTree();
}
}
}
}
}