8

I'm having an issue dragging a file from Windows Explorer on to a Windows Forms application.

It works fine when I drag text, but for some reason it is not recognizing the file. Here is my test code:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {

        }

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

AllowDrop is set to true on Form1, and as I mentioned, it works if I drag text on to the form, just not an actual file.

I'm using Vista 64-bit ... not sure if that is part of the problem.

Matt Hamilton
  • 200,371
  • 61
  • 386
  • 320
mattruma
  • 16,589
  • 32
  • 107
  • 171

4 Answers4

20

The problem comes from Vista's UAC. DevStudio is running as administrator, but explorer is running as a regular user. When you drag a file from explorer and drop it on your DevStudio hosted application, that is the same as a non-privileged user trying to communicate with a privileged user. It's not allowed.

This will probably not show up when you run the app outside of the debugger. Unless you run it as an administrator there (or if Vista auto-detects that it's an installer/setup app).

You could also run explorer as an admin, at least for testing. Or disable UAC (which I would not recommend, since you really want to catch these issues during development, not during deployment!)

Gene
  • 286
  • 1
  • 8
  • Well, that answers a problem that I've been having with the application that I've been working on. A good workaround that I've found that allows you to debug the application and still have drag & drop work properly is to run the application outside the debugger, and then attach the debugger to the process. – RobH May 06 '11 at 20:36
  • In cases where you don't need Visual Studio to run elevated, you can also just run it in normal mode to test the drag drop. However, I would suggest install VSCommands. It is an extension that allows you to run an explorer window as admin as well as some other neat stuff, like auto-selecting the related IIS instances to attach to when debugging -- among other things. – Timothy Lee Russell Oct 14 '13 at 17:38
0

Did you try to add the STAThread attribute to the main method?

  [STAThread]
  static void Main(string[] args)
  {
  }

I had the same problem as @mattruma meaning i got not Drag&Drop events. After adding the STAThread attribute to the main method it worked as expected.

k3b
  • 14,517
  • 7
  • 53
  • 85
0

The code you posted should work.

Try putting this at the beginning of the DragEnter method

string formats = string.Join( "\n", e.Data.GetFormats(false) );
MessageBox.Show( formats );

which will dump data formats associated with the d'n'd operation. Might help us narrowing down where the problem lies.

arul
  • 13,998
  • 1
  • 57
  • 77
0

I added the code that arul mentioned and things still didn't work, but it got me thinking.

I started thinking it might be a Vista issue so I sent it to a friend that had Windows XP and it worked great! I then tried running it outside of the Release folder in the bin directory and what do you know it worked!

The only time it does not work is when I am running it inside the Visual Studio 2008 IDE ... that's just weird.

Community
  • 1
  • 1
mattruma
  • 16,589
  • 32
  • 107
  • 171