拖放在 C# 中不起作用 [英] Drag and drop not working in C#

查看:28
本文介绍了拖放在 C# 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C# 中创建了一个拖放控件,以允许人们将文件拖放到我的表单上.这是我遇到的问题,它在调试时工作正常;但是,在管理员模式下运行我的程序时,它不起作用.这有什么原因吗?

I created a drag and drop control within C# to allow people to drop files onto my form. Here's the problem I'm having, it works fine when it's being debugged; however when running my program in administrator mode it doesn't work. Is there any reason for this?

这是我的代码:

private void panel1_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
        e.Effect = DragDropEffects.Copy;
    else
        e.Effect = DragDropEffects.None;
}

string startDir;

private void panel1_DragDrop(object sender, DragEventArgs e)
{
    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
    dropZoneLabel.Text = "Adding files; please wait...";
    foreach (string file in files)
    {
        bool isFolder = File.GetAttributes(file).HasFlag(FileAttributes.Directory);
        if (isFolder)
        {
            //Scan the folder for all files
            DirectoryOperations searchFolders = new DirectoryOperations();
            DirectoryInfo di = new DirectoryInfo(file);
            foreach (FileInfo dropfile in searchFolders.FullDirList(di, "*"))
            {
                listBox1.Items.Add(dropfile.Name);
            }
            startDir = di.FullName;
        }
        else
        {
            //It's a file so add it as normal
            listBox1.Items.Add(file);
        }
    }
    dropZoneLabel.Text = "Drop files or folders here";
}

推荐答案

从 Windows Vista 开始,由于用户界面特权隔离,您无法从运行在较低完整性级别的应用程序拖放到运行在较高级别的应用程序.

Starting from Windows Vista because of User Interface Privilege Isolation you cannot drag and drop from an application running at lower integrity level to an application which runs on a higher level.

有关详细信息,请参阅本文:为什么当我的应用程序运行提升时拖放不起作用?

See this article for more details: Why Doesn’t Drag-and-Drop work when my Application is Running Elevated?

这篇关于拖放在 C# 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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