Drag & Drop Folder Name from Windows Explorer to a Windows Form

  • Thread starter Thread starter David McKenzie
  • Start date Start date
D

David McKenzie

The title says it all.
What I would like to be able to do is drag a folder from Windows explorer on
to a form and just capture the full path and folder name.
Failing the fully qualified path, I absolutely need the folder name (which I
could find by iterating throught the folder structure)

All the doc I can find refers to dropping the data associated with an
object - and the Help topic
"Performing Drag-and-Drop Operations in Windows Forms"
Has not done much for me.
Am I missing something here?
Is what I want to do even possible?
 
Hi David,

Thanks for your post.

What you want is the windows shell drag&drop support. Currently, there is
no build-in support for doing shell drag&drop in Winform. However, we can
p/invoke some Shell API to get this done.

Below is a little sample I wrote to demonstrate what you want(we can place
this code snippet in any form class):

private const int WS_EX_ACCEPTFILES=0x00000010;
protected override CreateParams CreateParams
{
get
{
CreateParams cp=base.CreateParams;
cp.ExStyle=cp.ExStyle|WS_EX_ACCEPTFILES;
return cp;
}
}

[DllImport("shell32.dll", CharSet=CharSet.Auto)]
public static extern int DragQueryFile(IntPtr hDrop, int iFile,
StringBuilder lpszFile, int cch);

[DllImport("shell32.dll")]
static extern void DragFinish(IntPtr hDrop);

private const int WM_DROPFILES=0x0233;
protected override void WndProc(ref Message m)
{
if(m.Msg==WM_DROPFILES)
{
int iNumChars=DragQueryFile(m.WParam , 0, null, 0);
StringBuilder szDropFilePath = new StringBuilder(iNumChars+1);

DragQueryFile(m.WParam , 0, szDropFilePath, iNumChars+1);

DragFinish(m.WParam );
this.Text=szDropFilePath.ToString();
}
base.WndProc (ref m);
}

First, we add WS_EX_ACCEPTFILES window extended style to our form, this
will enable our form to accept shell file drop and display an accept icon
for dragging.

Whenever a file is dragged over our form, WM_DROPFILES will be posted to
the form WndProc. In this notificaiton, we can P/invoke DragQueryFile Shell
API to get the dropped files collection. Currently, I just pass 0 for the
second parameter to get the first file in the dropped file collection. For
more detailed usage of this API, please search it in MSDN documentation.

Hope this helps.
===================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
test that, that should gives you some idea....

public partial class Form1 : Form
{
public Form1()
{
AllowDrop = true;
}
protected override void OnDragEnter(DragEventArgs drgevent)
{
base.OnDragEnter(drgevent);
string[] fmt = drgevent.Data.GetFormats();
foreach (string s in fmt)
Console.WriteLine(s);
Console.WriteLine(drgevent.Data.GetData(DataFormats.FileDrop));
}
}

And remember, use the force, Oops, I mean the debugger is your friend!
 
Thanks Jefferey and Lloyd for the quick responses to myt first post in this
forum.
I will check ithis and post back presently.
 
Hi David,

You are welcome. Please feel free to feedback any progress here, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top