Distinguish between folder drop and file drop

  • Thread starter Thread starter Silvester
  • Start date Start date
S

Silvester

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 !
 
hi silvester, can you paste some example code?... any way...
when you fire any event you must have an eventhandler object and a
sender object.
explore what you get at sender.ToString(), or typeOf(sender)...

maybe that helps, have a nice day...

Silvester ha escrito:
 
You may want to start off by looking at the documentation on
FileSystemWatcher.OnCreated

One way to diffrentiate betweeb a file or folder is created/copied to your
watching folder

You may first try to open the newly created object with System.IO.File if
that throws an exception it should indicate that is not a file. Than attempt
opening the same object with System.IO.Directory. If either of these 2
attempts fails should be an indicationg it neither a file nor a dir.

Hope this helps.
 
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
 
Back
Top