从Windows文件浏览器拖放到Windows窗体不起作用 [英] Drag and drop from Windows File Explorer onto a Windows Form is not working

查看:211
本文介绍了从Windows文件浏览器拖放到Windows窗体不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将文件从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在Form1上设置为true,正如我所提到的,如果我将文本拖动到表单上,而不是一个实际的文件,它可以工作。

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 -bit ...不确定是否是问题的一部分。

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆