Drag and drop from Windows File Explorer onto a Windows Form is not working

余生长醉 提交于 2019-12-03 06:59:35

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!)

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.

mattruma

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!