469,592 Members | 1,801 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

Distinguish between folder drop and file drop

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 !
Oct 21 '06 #1
3 3089
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:
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 !
Oct 21 '06 #2
MMA
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" wrote:
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 !
Oct 21 '06 #3
Silvester wrote:
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<stringfiles = new List<string>();
ICollection<stringfolders = 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<stringfiles, ICollection<stringfolders)
{
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<stringfiles = new List<string>();
ICollection<stringfolders = 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
--
You can email me directly by removing the NOSPAm below
xm**********@gmxNOSPAm.netNOSPAm
Oct 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by pradeep_TP | last post: by
1 post views Thread by Larry Bates | last post: by
reply views Thread by Mailing List SVR | last post: by
reply views Thread by Fredrik Lundh | last post: by
9 posts views Thread by Keith G Hicks | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.