19

How could I load files to a form by drag-and-drop?

Which event will appear?

Which component should I use?

And how to determine name of file and others properties after drag-and-dropping it to a form?

Thank you!

Code

   private void panel1_DragEnter(object sender, DragEventsArgs e){
        if (e.Data.GetDataPresent(DataFormats.Text)){
              e.Effect = DragDropEffects.Move;
              MessageBox.Show(e.Data.GetData(DataFormats.Text).toString());
        }
        if (e.Data.GetDataPresent(DataFormats.FileDrop)){

        }
   }

ok, this works.

How about files? How can I get filename and extension?

and this is only a dragEnter action.

gaussblurinc
  • 3,642
  • 9
  • 35
  • 64
  • The various drag-related events are all handled together in a coordinated fashion. Have you read the relevant documentation? – Cody Gray - on strike Dec 18 '11 at 09:56
  • 1
    possible duplicate of [How do I drag and drop files into a c# application?](http://stackoverflow.com/questions/68598/how-do-i-drag-and-drop-files-into-a-c-sharp-application) – Hans Passant Dec 18 '11 at 10:25

3 Answers3

34

This code will loop through and print the full names (including extensions) of all the files dragged into your window:

if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
      string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
      foreach (string filePath in files) 
      {
          Console.WriteLine(filePath);
      }
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Sarwar Erfan
  • 18,034
  • 5
  • 46
  • 57
5

Check the below link for more info

http://doitdotnet.wordpress.com/2011/12/18/drag-and-drop-files-into-winforms/

private void Form2_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
      string[] filePaths = (string[])(e.Data.GetData(DataFormats.FileDrop));
      foreach (string fileLoc in filePaths)
        // Code to read the contents of the text file
        if (File.Exists(fileLoc))
          using (TextReader tr = new StreamReader(fileLoc))
          {
            MessageBox.Show(tr.ReadToEnd());
          }
    }
}
René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • 1
    A summary of the contents of the link is required for a quality answer. And be careful with posting links to your own blog: it looks an awful lot like spam. – Cody Gray - on strike Dec 18 '11 at 10:08
4

You need work with 2 below Events, of course while your Control/Form AllowDrop property's is true.

    private void Home_DragOver(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
            e.Effect = DragDropEffects.Link;
        else
            e.Effect = DragDropEffects.None;
    }

    private void Home_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            //YourOpenMethod(files);
        }
    }

Enjoy...

MiMFa
  • 981
  • 11
  • 14