2

For example, I want to drop a file "C:\test.txt" into Notepad. I used the following code. (An existing answer used DragDrop.DoDragDrop instead, but that seemed to belong to WPF.)

private void label1_MouseDown(object sender, MouseEventArgs e)
{
    var file = new DataObject(DataFormats.FileDrop, new string[] { @"C:\test.txt" });

    label1.DoDragDrop(file, DragDropEffects.Copy | DragDropEffects.Move);
}

"Notepad" did not seem to get the file drop. It did show the [+] cursor, but when I dropped it, nothing happened. I tried with "Notepad++"; it did not work either. It worked with "WordPad" and "HxD", however. What is wrong with the code above?


It seems that x86/x64 is the issue. Notepad and Notepad++ on my system are 64-bit versions. My app was compiled for x86 by the default setting. When I compiled my app for x64, dragging file to Notepad worked.

Community
  • 1
  • 1
Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135
  • I have just tried your code and dragged it into the notepad++ window and its opened the file so your code works. Are you trying to drag it onto the taskbar or into the application – Simon Price Jan 28 '17 at 10:12
  • I dropped it into running application windows, not to the icons. Did it also work with "Notepad"? – Damn Vegetables Jan 28 '17 at 10:15
  • no, it doesnt work with notepad, but then notepad has never had the ability to drop files into it – Simon Price Jan 28 '17 at 10:17
  • As far as I remember, dragging a file from File Explorer (Windows Explorer) into "Notepad" has always worked... at least from Windows 98. Are you sure that "Notepad" does not accept file drop? – Damn Vegetables Jan 28 '17 at 10:19
  • i stand corrected, yes you can from file explorer – Simon Price Jan 28 '17 at 10:20
  • It seems that x86/x64 is the issue. Notepad and Notepad++ on my system are 64-bit versions. My app was compiled for x86 by the default setting. When I compiled my app for x64, dragging file to Notepad worked. – Damn Vegetables Jan 28 '17 at 10:31
  • 1
    Hmm, strange problem. The only thing I see wrong is that "vegetable" is spelled wrong. It is not bitness, Wordpad is a 64-bit process. Some strange quirk with Notepad, I guess. Being as old as it is, it might still be using WM_DROPFILES. You need to document your Windows version. – Hans Passant Jan 28 '17 at 10:56
  • I stand corrected (about vegetable). I saw that WordPad was also 64-bit. I thought that WordPad might have some extra code to get drag from a 32-bit process, and other 64-bit applications (Notepad, Notepad++) had not. My Windows version is 64-bit Windows 10. – Damn Vegetables Jan 28 '17 at 11:19

0 Answers0