Drag & Drop ... getting a handle to the file that was dropped.

G

Guest

We are attempting to allow our users to use Drag n Drop onto a control in a
WinForms v2 application.

Within the control_DragDrop() event handler we want to determine the name
and location for a file that was dropped onto this control. We are
moderately familiar with performing DnD from within our application - we do
this between various controls within the app itself - but, a lot to learn
when it comes to DnD and external files from outside our application.

We are successfully determining that the user is trying to drop a file onto
the control with the following code in the DragEnter() event handler:

if (e.Data.GetDataPresent( DataFormats.FileDrop ))
{
Console.WriteLine("A file was dropped .......");
}

What we're trying to do now, is create a File object (or something similar
that you might recommend) using the file the user has dropped so that we can
get to its contents, file name, date it was created, etc., etc. Just not
sure how to get the handle to the file??

Thanks
 
C

Chris Taylor

Hi,

If I understand your question correctly, you can ue the GetData method, for
FileDrop it will return a string[] of the file paths.
string[] filePaths = e.Data.GetData(DataFormats.FileDrop)

Hope this helps
 
G

Guest

Thanks Chris - that works perfect!!

I feel a bit silly for not having thought this through better ... I noticed
yesterday when I was messing around and experimenting with this code that the
toString() method resulted in a string array ... didn't even dawn on me that
these might be the file paths.

We use "Windows Forms 2.0 Programming" by Sells as a bible on our desktops.
He has a good section on DnD ... even discusses "document management" in the
sense of being able to DnD your own file types back onto the form to open the
document. But in his code there's a limitation that we can only do this on
the "main" form (our design is a MDI style app) because he's using interop to
register and listen for the WM_DROPFILES message. By calling the
DragQueryFile function (both to determine how many files where dropped, then
again [different params the second call] to get the paths) we can do the same
thing - but with the limitation that this can only run on the main form.
Works great but we need this on the MDI child form, which - thank you - your
suggestion supports.

The "devil is in the details," as someone once said. Again, thanks Chris
.... really appreicate the help.

--
Stay Mobile


Chris Taylor said:
Hi,

If I understand your question correctly, you can ue the GetData method, for
FileDrop it will return a string[] of the file paths.
string[] filePaths = e.Data.GetData(DataFormats.FileDrop)

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
MobileMan said:
We are attempting to allow our users to use Drag n Drop onto a control in
a
WinForms v2 application.

Within the control_DragDrop() event handler we want to determine the name
and location for a file that was dropped onto this control. We are
moderately familiar with performing DnD from within our application - we
do
this between various controls within the app itself - but, a lot to learn
when it comes to DnD and external files from outside our application.

We are successfully determining that the user is trying to drop a file
onto
the control with the following code in the DragEnter() event handler:

if (e.Data.GetDataPresent( DataFormats.FileDrop ))
{
Console.WriteLine("A file was dropped .......");
}

What we're trying to do now, is create a File object (or something similar
that you might recommend) using the file the user has dropped so that we
can
get to its contents, file name, date it was created, etc., etc. Just not
sure how to get the handle to the file??

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top