Silvester said:
I need to trigger some code when user drags and drops an entire folder onto
my form.
How can I distinguish between a folder drop and files dropped ? Can someone
point me to code or a sample, please ?
Thanks !
If I understand it right, the folders will be dragged onto your form
from the windows explorer. Then the yode below should give you a good start:
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnDragOver(DragEventArgs drgevent)
{
ICollection<string> files = new List<string>();
ICollection<string> folders = new List<string>();
ExtractDragDropData(drgevent, files, folders);
// is it file/folder d&d?
if (files.Count == 0 && folders.Count == 0)
return;
// Set D&D cursor depending on what is inside the d&d data
if (files.Count > 0)
drgevent.Effect = DragDropEffects.Move;
else if (folders.Count > 0)
drgevent.Effect = DragDropEffects.Copy;
base.OnDragOver(drgevent);
}
private static void ExtractDragDropData(DragEventArgs drgevent,
ICollection<string> files, ICollection<string> folders)
{
foreach (string fileOrFolder in
(string[])drgevent.Data.GetData("FileDrop"))
{
if (Directory.Exists(fileOrFolder))
folders.Add(fileOrFolder);
else if(File.Exists(fileOrFolder))
files.Add(fileOrFolder);
else
{
// Something completely diferent
}
}
}
protected override void OnDragDrop(DragEventArgs drgevent)
{
ICollection<string> files = new List<string>();
ICollection<string> folders = new List<string>();
ExtractDragDropData(drgevent, files, folders);
MessageBox.Show(string.Format("You have dropped {0} files
and {1} folders", files.Count, folders.Count));
base.OnDragDrop(drgevent);
}
}
}
HTH,
Andy