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

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

问题描述

我在C#中创建了一个拖放控件,让人们将文件放在我的表单上。这是我遇到的问题,它被调试时工作正常;然而,当以管理员模式运行程序时,它不起作用。这是否有理由?



这是我的代码:

  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 =添加文件,请稍候...;
foreach(文件中的字符串文件)
{
bool isFolder = File.GetAttributes(file).HasFlag(FileAttributes.Directory);
if(isFolder)
{
//扫描所有文件的文件夹
DirectoryOperations searchFolders = new DirectoryOperations();
DirectoryInfo di = new DirectoryInfo(file);
foreach(SearchFolders.FullDirList(di,*)中的FileInfo dropfile)
{
listBox1.Items.Add(dropfile.Name);
}
startDir = di.FullName;
}
else
{
//这是一个文件,因此正常添加它
listBox1.Items.Add(file);
}
}
dropZoneLabel.Text =在这里删除文件或文件夹;
}


解决方案

用户界面特权隔离您不能从运行在较低完整性级别的应用程序拖放到在较高级别运行的应用程序。



有关详细信息,请参阅此文章: a href =http://blogs.msdn.com/b/patricka/archive/2010/01/28/q-why-doesn-t-drag-and-drop-work-when-my-application-is-运行提升的一个强制完整性控制和uipi.aspx>为什么我的应用程序运行升高时不拖放工作?


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?

Here's my code:

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";
}

解决方案

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天全站免登陆