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

后端 未结 4 1753
一生所求
一生所求 2021-02-09 01:41

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

相关标签:
4条回答
  • 2021-02-09 02:28

    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.

    0 讨论(0)
  • 2021-02-09 02:38

    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.

    0 讨论(0)
  • 2021-02-09 02:38

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

    0 讨论(0)
  • 2021-02-09 02:40

    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.

    0 讨论(0)
提交回复
热议问题