从Windows资源管理器到Windows窗体拖放不工作 [英] Drag and drop from Windows File Explorer onto a Windows Form is not working
问题描述
我有一个问题,从Windows资源管理器将文件拖动到Windows窗体应用程序。
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设置为true Form1上,正如我所提到的,它的工作原理,如果我拖动文本上的形式,只是没有实际的文件。
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.
我使用的是Vista的64比特...不知道如果这是问题的一部分。
I'm using Vista 64-bit ... not sure if that is part of the problem.
推荐答案
这个问题来自Vista的的UAC 。 DevStudio的是管理员身份运行,但探险家正在运行作为一个普通用户。当您从资源管理器将文件拖放到您DevStudio的托管应用,也就是同一个非特权用户试图用特权用户进行通信。这是不允许的。
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.
这可能不会显示出来,当你运行应用程序的调试程序之外。除非你运行它以管理员身份出现(或者Vista的自动检测,它的安装程序/设置应用)。
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).
您也可以运行资源管理器作为管理员时,至少进行检测。或禁用UAC(我不会推荐,因为你真的想在开发过程中捕获这些问题,而不是在部署过程中!)
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!)
这篇关于从Windows资源管理器到Windows窗体拖放不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!