Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the path of a file dragged into a Windows Forms form

I am developing an application which requires the user to drag a file from Windows Explorer into the application window (Windows Forms form). Is there a way to read the file name, path and other properties of the file in C#?

like image 645
matrix Avatar asked Dec 06 '10 08:12

matrix


2 Answers

You can catch the DragDrop event and get the files from there. Something like:

void Form_DragDrop(object sender, DragEventArgs e) {     string[] fileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);      //more processing } 
like image 75
Adrian Fâciu Avatar answered Oct 17 '22 19:10

Adrian Fâciu


you Should Use Two Events 1) DragDrop 2) DragEnter

Also enable "AllowDrop" Property of Panel/form to true.

 private void form_DragEnter(object sender, DragEventArgs e)     {         if (e.Data.GetDataPresent(DataFormats.FileDrop))         {             e.Effect = DragDropEffects.Copy;         }         else         {             e.Effect = DragDropEffects.None;         }     }      private void form_DragDrop(object sender, DragEventArgs e)     {         string[] filePaths= (string[])e.Data.GetData(DataFormats.FileDrop, false);     } 
like image 24
Sayed Muhammad Idrees Avatar answered Oct 17 '22 17:10

Sayed Muhammad Idrees